如何为 Twig 模板指定两个位置? [英] How do I specify two locations for Twig templates?
问题描述
我为每个组件(即页面)使用单独的目录构建了我的网站.
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/
在每个目录中,我有一个控制器、一个模型和一个 Twig 模板.可能位于 /var/www/components/component_2/templates/myTemp.html
的典型模板如下所示:
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
我有几个基本模板.与子模板不同,我希望它们位于 /var/www/templates/
I have several base templates. Unlike the child templates, I wish these to be located in /var/www/templates/
如何在扩展 Twig 模板时指定两个位置?
How do I specify two locations when extending Twig templates?
推荐答案
看看 Twig Loader Filesystem http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem
Just take a look at Twig Loader Filesystem http://twig.sensiolabs.org/doc/api.html#twig-loader-filesystem
所以可以这样做:
$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'));
这篇关于如何为 Twig 模板指定两个位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!