How do I specify two locations for Twig templates?

五迷三道 提交于 2020-01-15 12:33:32

问题


I've structured my website with a separate directory for each component (i.e. page).

/var/www/components/component_1/
/var/www/components/component_2/
/var/www/components/component_3/

Within each directory, I have a controller, a model, and a Twig template. A typical template which might be located at /var/www/components/component_2/templates/myTemp.html looks like the following:

{% extends "base.html" %}

bla bla bla

I have several base templates. Unlike the child templates, I wish these to be located in /var/www/templates/

How do I specify two locations when extending Twig templates?


回答1:


Just take a look at Twig Loader Filesystem http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem

So can do something like:

$loader = new Twig_Loader_Filesystem(array('/var/www/components/component_1/templates', '/var/www/templates'));
$twig = new Twig_Environment($loader);

echo $twig->render('Hello {{ name }}!', array('name' => 'Fabien'));


来源:https://stackoverflow.com/questions/25974568/how-do-i-specify-two-locations-for-twig-templates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!