简体   繁体   English

如何在Twig循环中连接两个Twig变量

[英]How to concatenate two Twig variables in a Twig loop

I have a simple loop which requires me to concatenate my loop counter variable loop.index within my main value variable (hope that makes sense) but I can't get it working. 我有一个简单的循环,它要求我在我的主值变量中连接我的循环计数器变量loop.index (希望这是有道理的)但我不能让它工作。

Is it even possible? 它甚至可能吗? See below... 见下文...

{% for article in section.articles %}
    {{ article.internationalText~{{loop.index}} |raw|nl2br }}
{% endfor %}

You can use twigs attribute function which was added in version 1.2. 您可以使用版本1.2中添加的twigs 属性函数。 It is designed for accessing a "dynamic" attribute of a variable. 它旨在访问变量的“动态”属性。

{% for article in section.articles %}

    {{ attribute(article, 'internationalText' ~ loop.index) |raw|nl2br }}

{% endfor %}

Note, benatespina's answer did not work for me. 请注意,benatespina的回答对我不起作用。

Have you tried this? 你试过这个吗?

{% for article in section.articles %}

    {{ article.internationalText~loop.index |raw|nl2br }}

{% endfor %}

This should work. 这应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM