Bob Flemming
Bob Flemming

Reputation: 466

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.

Is it even possible? See below...

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

Upvotes: 3

Views: 6509

Answers (2)

idavidmcdonald
idavidmcdonald

Reputation: 103

You can use twigs attribute function which was added in version 1.2. 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.

Upvotes: 4

user2359967
user2359967

Reputation:

Have you tried this?

{% for article in section.articles %}

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

{% endfor %}

This should work.

Upvotes: 2

Related Questions