arlg
arlg

Reputation: 1138

Using global variables inside template includes

I need to use a global defined variable, $LANG, inside an include so that I can select the good file according to the user language.

My variable is defined like this :

// File : index.php
// Twig is defined earlier
$twig->addGlobal('lang', $LANG);

// File : body.html
<div id="svg">
    {% include 'svg/mysvg-'.lang.'.svg' %}
</div> 

Can I use the variable as I tried to use it in the include? It doesn't work like this.

Upvotes: 0

Views: 358

Answers (2)

arlg
arlg

Reputation: 1138

I found how to do it :

{% include  (lang == 'fr') ? 'svg/mysvg-fr.svg' : 'svg/mysvg-de.svg' %}    

via : http://twig.sensiolabs.org/doc/tags/include.html

Upvotes: 0

putvande
putvande

Reputation: 15213

I think you should just be able to do it the way you did it in your question. But concatenating strings in Twig is not with . byt with ~.

{% include 'svg/mysvg-' ~ lang ~ '.svg' %}

Upvotes: 1

Related Questions