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 locale
by 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
对象中获取
归档时间: |
|
查看次数: |
748 次 |
最近记录: |