Tomazi
Tomazi

Reputation: 847

path to twig template in Symfony 2

I am working on a Symfony2 project, currently trying to render a view for my controller but annoyingly I am constantly seeing this error Message:

Unable to find template

So to my understanding there is an issue with the path part of the render function. I tried tackling this but no matter how I change my path I allways get this message.

Full path to the test.html.twig file:

src/Test/NewsInfrastructure/Sitemap/test.html.twig

This is how I implemente this path:

return $this->templating->renderResponse('src:Test:NewsInfrastructure:Sitemap:test.html.twig', array('sitemap' => $this->siteMap->getSiteMap()));

What is it that I am doing wrong...?

Upvotes: 1

Views: 5050

Answers (1)

xurshid29
xurshid29

Reputation: 4210

TwigBundle (TwigExtension) searches the templates through Bundle's folder (Resources/views) or upper levels;

I think the easiest way to render a template by calling it by namespace, like this:

$this->render('@Vendor/Product/view.html.twig'); // src/Vendor/MyBundle/Resources/views/Product/view.html.twig

@Vendor - without Bundle suffix, also it works bit faster..

Or

This can help you if you really want to load the template wich locates outside of Resources folder:

$loader = new \Twig_Loader_Filesystem(PATH_TO_DIRECTORY);
$twig = new \Twig_Environment($loader, array('cache' => false));    
$twig_code = $twig->render('myFile.html.twig', []);

return new Response($twig_code);

Upvotes: 2

Related Questions