Harmstra
Harmstra

Reputation: 461

concat string and variable in twig

I have this in my template

    {{ ad.title_de }}

Now I'm incorporating multiple languages, so 'title_de' has to change I also have a variable 'tld' which is one of de, en , fr

So I'd like to have something like

    {% if tld == 'fr' %} 
    {{ ad.title_fr }} 

etc Any ideas?

Upvotes: 1

Views: 2029

Answers (2)

Jacob
Jacob

Reputation: 1793

Try using the attribute function.

http://twig.sensiolabs.org/doc/functions/attribute.html

attribute(ad, 'content_'~tld) should work.

Upvotes: 1

mwl
mwl

Reputation: 1488

Try with this:

{{ ad["title_" ~ tld] }}

Upvotes: 0

Related Questions