Search code examples
phpsymfonysonata-adminsymfony-3.3

Symfony: error while rendering block - Unable to find template


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?


Solution

  • 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.