Symfony does not search in package for twig files

I created a new package that will include twig files. By default, there is already a twig file added by the command line of the generated package (in src/TestBundle/Resources/views/test.html.twig ). The render-command has also been added to IndexAction:

return $this->render('TestBundle:test.html.twig', $data);

But when I call IndexAction, I get a template of an error not found, and the error says that symfony only looked in

 /app/Resources/views, /vendor/symfony/symfony/src/Symfony/Bridge/Twig/Resources/views/Form 

but not in /src/TestBundle/...

Why does symfony ignore package resources? And how can I change that?

+5
source share
3 answers

I had the same problem with Symfony 3.3, and as a result, through the trial version and error, you found that you need to use @ followed by the package name, but not specify the word β€œBundle” from the namespace. So that...

 @MyFabulousUserBundle/Email/reset_password.html.twig 

... does not work, but:

 @MyFabulousUser/Email/reset_password.html.twig 

did!

+4
source

You must not remove the subfolder separator.

When the template is in

TestBundle / Resources / view /

Do it

 return $this->render('TestBundle::test.html.twig', $data); 

When the template is in a subfolder, for example

TestBundle / Resources / views / folders /

Use this

 return $this->render('TestBundle:Folder:test.html.twig', $data); 

You can read the document here.

0
source

I also had a problem with SF 3.3. I think the error message is general, Symfony does not list your custom packages in the error message when it cannot find the template.

In my case, it was just that I incorrectly named the folder

 $twig->render("UserBundle:Email:registrationComplete.html.twig") 

I just missed the 's' in Email, since my folder was named Email.

Therefore, make sure that the specified template path exists. Sometimes it can also be a cache if you just added a template.

0
source

Source: https://habr.com/ru/post/1260785/


All Articles