Php 在include中设置Twig变量,然后使用它
假设您有一个包含另一个模板的小树枝模板(tplA.twig)Php 在include中设置Twig变量,然后使用它,php,global-variables,twig,template-engine,Php,Global Variables,Twig,Template Engine,假设您有一个包含另一个模板的小树枝模板(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> 细枝模板A {%set test=“在tpl A中”
<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>
细枝模板A
{%set test=“在tpl A中”%}
首先,变量是:{test}
{%include“tplB.twig”%}
变量是:{test}
以及包含的模板(tplB.twig)
第三方物流B的内容
{%set test=“现在是tpl B”%}
在包含的模板中设置/更改变量并在主模板中使用它的最佳方法是什么?如何使用globals?请注意,我不能使用块和扩展,也没有使用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>
{{i18n('mainttitle')}
当前语言(fr,en):{{ln}
局部A
局部B
这是一个非常小的网站,所以我没有用数据库创建复杂的结构,我想在模板中管理所有这些内容
问题是:如何显示正确的本地化字符串?我考虑的是包含一个本地化模板,以分离关注点。比如:
<!-- before the photos list -->
{% include ln ~ '/photos-list.twig' %}
<!-- then the photos list -->
{%include ln~'/photos list.twig%}
在这个模板中,我会将所有变量设置在正确的区域设置中,这样我就可以在我的照片列表中使用它们。(如问题第一部分所述)
请注意,我对所有其他页面都有这种结构。文本内容在区域设置文件夹中分开,每个页面都有一个主模板(同样,它是一个非常基本的个人网站)
我最后做的是在照片列表前插入一个hudgeif语句。如果locale是fr,用法语文本设置变量,如果是en,用英语文本设置变量
这就是诀窍,所以我同意^ ^为什么不在
tplB
中移动{{test}
的第二个调用(在这种情况下)
第三方物流B的内容
{%set test=“现在是tpl B”%}
变量是:{test}可能重复
<!-- before the photos list -->
{% include ln ~ '/photos-list.twig' %}
<!-- then the photos list -->