2014-10-17 60 views
4

想象您有包括树枝模板(tplA.twig)的另一种内的集枝杈变量包括,用它之后

<p>Twig template A</p> 

{% set test = "in tpl A" %} 
<p>first, variable is: {{ test }}</p> 

{% include "tplB.twig" %} 
<p>and the variable is: {{ test }}</p> 

和包含的模板(tplB.twig)

<div>Content of tpl B</div> 
{% set test = "now is tpl B" %} 

设置/更改包含模板中的变量并将其用于主模板中的最佳方法是什么?如何使用全局变量?请注意,我不能使用块和扩展,并且我没有使用Symfony。

非常感谢

编辑

真正的上下文是非常基本多种语言结构。对于一个页面,我有一个主模板:

<h1>{{ i18n('mainTitle') }}</h1> 
<h2>current language (fr, en): {{ ln }}</h2> 

<!-- here a list of photos, but not in a foreach loop --> 
<div> 
    <div> 
     <img src="/same/for/all/languages-a.jpg" alt="localized A" /> 
     <span>localized A</span> 
    </div> 
    <div> 
     <img src="/same/for/all/languages-b.jpg" alt="localized B" /> 
     <span>localized B</span> 
    </div> 
    <!-- etc. --> 
</div> 

这是一个非常小的网站,所以我没有创建与数据库中的结构复杂,而我想管理模板这一切的东西。

问题是:如何显示正确的本地化字符串?我想到的是包含一个本地化模板,以分离关注点。喜欢的东西:

<!-- before the photos list --> 
{% include ln ~ '/photos-list.twig' %} 
<!-- then the photos list --> 

而这个模板中,我会在设置好的正确的语言环境的变量,所以我可以在我的照片列表中使用它们。 (如我的问题的第一部分所述)

请注意,我对所有其他页面都有此结构。文本内容的语言文件夹分开,并且每个页面都有一个主模板(再次,这是一个非常基本的个人网站)

我也终于是插入图片列表前hudge 如果声明。如果区域设置为fr,则用法语文本设置变量,如果它是en,则用英文文本设置变量。

这是卓有成效的,所以我确定与^^

+0

可能重复http://stackoverflow.com/questions/36919869/include-a-twig-file-and-pass-variables-from-a-separate-file/37011064#37011064 – 2016-05-06 16:18:35

回答

0

你为什么不动的{{ test }}tplB第二个电话(此场合)?

<div>Content of tpl B</div> {% set test = "now is tpl B" %} <p>and the variable is: {{ test }}</p>

+0

感谢,我编辑我的问题要更清楚一些 – rekam 2016-12-06 13:22:57