Zag*_*loo 0 concatenation symfony twig
我必须将两个变量连接在一起,旁边是 request.locale
我向你解释一下:
我有一个实体命名的Lexicon几个领域:
wordFr,wordEn,definitionFr,definitionEn
我尝试做类似的事情来替换Fr或En根据request.locale它但它不起作用:
{% set locale = '' %}
{% if app.request.locale == "fr" %}
{% set locale = 'Fr' %}
{% else %}
{% set locale = 'En' %}
{% endif %}
{% for wordList in wordsList %}
<tr>
<td>{{ wordList.word~locale }}</td>
<td>{{ wordList.definition~locale }}</td>
</tr>
{% endfor %}
Run Code Online (Sandbox Code Playgroud)
如何拥有{{ wordList.wordFr }}或{{ wordList.wordEn }}根据语言环境(替换var localeby Fr或En)?谢谢 !
与此同时,我做了这个,但它太长,重复......
{% if app.request.locale == "fr" %}
{% for listeMots in listeMotsLexique %}
<tr>
<td>{{ wordList.wordFr }}</td>
<td>{{ wordList.definitionFr }}</td>
</tr>
{% endfor %}
{% else %}
{% for listeMots in listeMotsLexique %}
<tr>
<td>{{ wordList.wordEn }}</td>
<td>{{ wordList.definitionEn }}</td>
</tr>
{% endfor %}
{% endif %}
Run Code Online (Sandbox Code Playgroud)
小智 5
你想要的是使用这里attribute记录的Twig 函数.
它允许您使用动态变量名称.你必须这样做:
{{ attribute(wordList, 'mot'~locale) }}
Run Code Online (Sandbox Code Playgroud)
你基本上是说'mot'~locale要从wordList对象中获取