1
votes

I am using Sonata's block bundle and Symfony 3.3 to develop an application. I have configured a block service for displaying YouTube videos.

The service includes the following code:

    $blockContext->setSetting('template', 'AppBundle:Components:videoElement.html.twig');
    return $this->renderResponse($blockContext->getTemplate(), array(
        'block' => $block,
        'settings' => $settings,
        'media' => $media,
        'framehtml' => $frameHtml,
        'provider_reference' => $providerReference,

    ), $response);

When I use the service on my laptop, it works well. When I shift to another environment, videos no longer display, and I get the following error:

[2018-04-05 11:24:23] app.ERROR: [cms::renderBlock] block.id=13 - error while rendering block - Unable to find template "AppBundle:Components:videoElement.html.twig" (looked into: /srv/htdocs/vgms-core/app/Resources/views, /srv/htdocs/vgms-core/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form, /srv/htdocs/vgms-core/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views). {"exception":"[object] (InvalidArgumentException(code: 0): Unable to find template \"AppBundle:Components:videoElement.html.twig\" (looked into: /srv/htdocs/vgms-core/app/Resources/views, /srv/htdocs/vgms-core/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form, /srv/htdocs/vgms-core/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views). at /srv/htdocs/vgms-core/vendor/symfony/symfony/src/Symfony/Bridge/Twig/TwigEngine.php:127, Twig_Error_Loader(code: 0): Unable to find template \"AppBundle:Components:videoElement.html.twig\" (looked into: /srv/htdocs/vgms-core/app/Resources/views, /srv/htdocs/vgms-core/vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form, /srv/htdocs/vgms-core/vendor/knplabs/knp-menu/src/Knp/Menu/Resources/views). at /srv/htdocs/vgms-core/vendor/twig/twig/lib/Twig/Loader/Filesystem.php:232)"} []

The template exists and lives in src/AppBundle/Resources/views/components/videoElement.html.twig.

How can I explicitly tell Symfony where to look for this template?

1
Where is your template into AppBundle or app/Resources/views?Imanali Mamadiev
Thanks, gp_sflover -- that worked. I accessed my template with @App/components/videoElement.html.twig which did the trick. If you want to expand this into an answer, I will mark it as the accepted one.Patrick

1 Answers

1
votes

As showed in Referencing Templates in a Bundle (Symfony 3.3 version) you can use the Twig syntax @BundleName/directory/filename.html.twig to specify where a specific bundle template lives.

By default specific bundle templates are stored in the BundleName/Resources/views folder and could be referred using @App/videoElement.html.twig, so in your case you only need to indicate the added components folder with @App/components/videoElement.html.twig.