我使用symfony 3 +树枝作为模板引擎。Twig动态创建字符串变量
我需要创建一个基于字符串变量,比如我想
{% set varName='Document' %}
,然后像
{% for createVariable(varName) in entities %}
为什么呢?
因为我有一个模板,其中包括另一个模板B. 在模板AI设定{% fileToInclude='...' %}
,在模板BI包括fileToInclude
需要一个特定的变量的工作,但fileToInclude
可以是需要另一个特定变量的另一个模板,所以我需要从我设置的字符串创建一个变量,所以如果我包含例如“document.html.twig”,那么我会设置varName ='Document',如果我包含'news.html.twig',我'将设置varName ='新闻'...
如何做到这一点?
编辑:
文件:
{% set includeFile='filec.html.twig %} //HERE I can set filed or filee
{% set varName='document' %}
{% include 'fileb.html.twig' %}
FILE B:
{% for entity in entities %} // HERE entity should be named document or news or what I need
{% include includeFile %}
{% endfor %}
FILE C:
{{ document.title }}
FILE d:
{{ news.text }}
你能澄清你的问题吗?是否要创建动态变量?如果是这样,我没有看到你的代码示例。 –
@AlvinBunk编辑问题 – Francesco
我直到不要打开你的问题。特别是'{%for createVariable(varName)in entities%}' – Alsatian