How does Symfony2 detect where to load templates f

2019-09-09 07:15发布

问题:

Where does Symfony2 detect where to load templates from: from app/Resourses/views/ or from Bundle/Resourses/views/?

I need to override it to look for templates in another directory before looking into app/Resourses/views/.

I found: Symfony\Bundle\FrameworkBundle\CacheWarmer\TemplateFinder

Now I just need to override it. How?

回答1:

Symfony2 works with this workflow

  1. Access route, pass control to controller, render a view (template)
  2. If the view is not overridden ( app/..../ ) then bundle template is render
  3. If there's an override, then took it

So the question is: how can I override a template?

There are two different ways for override templates in symfony2

  • Define a template with same name into app/resources directory. Together "same name" you have to reproduce the same structure of the bundle. So, if your template is in, i.e., myFooBundle/views/mainTemplate.html.twig you have to override it by creating a new one template in app/resources/myFooBundle/views and call it mainTemplate.html.twig
  • You have to create a new bundle from scratch and override getParent() method that have to return a string containing bundle name that you want to override ( myfooBundle ). Now if you create a new template in the same position of the original, you have overwritten it. This method is not recommended since you have to override controller also.


标签: symfony twig