This documentation is for a version that has reached its End Of Life. Such versions are no longer supported and don't receive security updates. Consider updating to a newer version.
While in PHP, you may need to render the view of a content item (for example, for further treatment like PDF conversion, or because you're not in an HTML context).
Caution
Avoid using PHP rendering in a controller as much as possible.
You can access a view directly through the route /view/content/{contentId}/{viewType}[/{location}].
For example, on a fresh installation, you can access /view/content/52/line which returns a small piece of HTML with a link to the content that could be used in Ajax.
If you need a controller to have additional information available in the template or to customize the Response object, define the controller in a view configuration as shown in Controllers, enhance the View object and return it.
The following example is a command outputting the render of a content for a view type in the terminal.
It works only if the view doesn't refer to the HTTP request.
It's compatible with the default installation views such as line or embed.
To go further with this example, you could add some dedicated views not outputting HTML but, for example, plain text, Symfony command styled text or Markdown.
It doesn't work with a full view when the page layout uses app.request, such as the out-of-the-box template.
Append to config/services.yaml the command as a service:
<?phpdeclare(strict_types=1);namespaceApp\Command;useIbexa\Core\MVC\Symfony\View\Builder\ContentViewBuilder;useIbexa\Core\MVC\Symfony\View\Renderer\TemplateRenderer;useSymfony\Component\Console\Command\Command;useSymfony\Component\Console\Input\InputInterface;useSymfony\Component\Console\Input\InputOption;useSymfony\Component\Console\Output\OutputInterface;classViewCommandextendsCommand{protectedstatic$defaultName='app:view';privateContentViewBuilder$contentViewBuilder;privateTemplateRenderer$templateRenderer;publicfunction__construct(ContentViewBuilder$contentViewBuilder,TemplateRenderer$templateRenderer){parent::__construct(self::$defaultName);$this->contentViewBuilder=$contentViewBuilder;$this->templateRenderer=$templateRenderer;}protectedfunctionconfigure():void{$this->setDescription('Render the view of a content item')->addOption('content-id','c',InputOption::VALUE_OPTIONAL,'Content ID')->addOption('location-id','l',InputOption::VALUE_OPTIONAL,'Location ID')->addOption('view-type','t',InputOption::VALUE_OPTIONAL,'View Type','line');}protectedfunctionexecute(InputInterface$input,OutputInterface$output):int{$contentId=$input->getOption('content-id');$locationId=$input->getOption('location-id');if(empty($contentId)&&empty($locationId)){thrownew\InvalidArgumentException('No Content ID nor Location ID given');}$viewParameters=['viewType'=>$input->getOption('view-type'),'_controller'=>'ibexa_content:viewAction',];if(!empty($locationId)){$viewParameters['locationId']=$locationId;}if(!empty($contentId)){$viewParameters['contentId']=$contentId;}// build view$contentView=$this->contentViewBuilder->buildView($viewParameters);// render view$renderedView=$this->templateRenderer->render($contentView);$output->writeln($renderedView);return0;}}
Caution
As Ibexa\Core\MVC\Symfony\View\Builder\ContentViewBuilder and Ibexa\Core\MVC\Symfony\View\Renderer\TemplateRenderer aren't part of the public PHP API's Ibexa\Contracts namespace, they might change without notice.