<p>In order to pull this off, you'll need to take advantage of the not very well  <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">documented</a> <code>_context</code> variable in Twig.  This should do it:</p>

<pre><code>{% set language = 'fr' %}

{% set foobar = _context[language ~ "_foobar"] is defined ? _context[language ~ "_foobar"] : null %}

{% if foobar %}
    {{ foobar.globalAddress }}
{% endif %}
</code></pre>

CC BY-SA 3.0
2016-01-22T08:06:10.323Z
tried it that way, but i get this in response:
Impossible to access an attribute ("globalAddress") on a string variable ("fr_foobar")
Stefan Friedrich
CC BY-SA 3.0
2016-01-22T18:31:24.893Z
Updated the answer and actually tested this one. :)
Brad Bell
CC BY-SA 3.0
2016-01-26T19:48:27.480Z
`_context` documentation, such as it is, is here: http://twig.sensiolabs.org/doc/tags/macro.html
Marion Newlevant
CC BY-SA 3.0
2016-01-26T21:45:04.560Z
@BradBell ...and I had hoped to see the first example where the `attribute` function works `attribute(foo, bar)` and the subscript syntax `foo[bar]` doesn't.
carlcs
CC BY-SA 3.0
2016-01-26T22:04:10.460Z
@MarionNewlevant nice... linked to it.
Brad Bell
CC BY-SA 3.0
2016-01-26T22:04:29.503Z
@carlcs yeah, tried several variations using `attribute` and couldn't get anything to work.
Brad Bell
CC BY-SA 3.0
2016-01-21T19:23:28.783Z
2016-01-26T22:03:47.177Z
2016-01-26T22:03:47.177Z
Brad Bell
<p>I'd like to create a variable from another variable. What I got right now is something like this:</p>

<pre><code>{% set language = fr %} 
{% if language == "en"  %}
   {% set fooAddress = en_foobar.globalAddress %} 
{% if language == "fr"  %}
   {% set fooAddress = fr_foobar.globalAddress %}
{% if language == "de"  %}
   {% set fooAddress = de_foobar.globalAddress %} 
</code></pre>

<p>What I'd like to do is:</p>

<pre><code>{% set fooAddress = language  ~ "_foobar.globalAddress" %}
</code></pre>

<p>No problem with plain PHP (<a href="http://php.net/manual/en/language.variables.variable.php" rel="nofollow">http://php.net/manual/en/language.variables.variable.php</a>) but is this possible to do with Twig?</p>

<templating><php><variable><set>
Variable variables in Twig
# Variable variables in Twig

(Asked by: username_0 on 2016-01-21)

I'd like to create a variable from another variable. What I got right now is something like this:
{% set language = fr %} 
{% if language == "en"  %}
   {% set fooAddress = en_foobar.globalAddress %} 
{% if language == "fr"  %}
   {% set fooAddress = fr_foobar.globalAddress %}
{% if language == "de"  %}
   {% set fooAddress = de_foobar.globalAddress %} 

What I'd like to do is:
{% set fooAddress = language  ~ "_foobar.globalAddress" %}

No problem with plain PHP (http://php.net/manual/en/language.variables.variable.php) but is this possible to do with Twig?

**Tags:** templating, php, variable, set

---

## 1 answers

### Answer by username_1 on 2016-01-21. Score: 4 | Accepted answer

In order to pull this off, you'll need to take advantage of the not very well  documented _context variable in Twig.  This should do it:
{% set language = 'fr' %}

{% set foobar = _context[language ~ "_foobar"] is defined ? _context[language ~ "_foobar"] : null %}

{% if foobar %}
    {{ foobar.globalAddress }}
{% endif %}

> **Comments:**
> **username_0**: tried it that way, but i get this in response: Impossible to access an attribute ("globalAddress") on a string variable ("fr_foobar")  
> **username_1**: Updated the answer and actually tested this one. :)  
> **username_2**: `_context` documentation, such as it is, is here: http://twig.sensiolabs.org/doc/tags/macro.html  
> **username_3**: @BradBell ...and I had hoped to see the first example where the `attribute` function works `attribute(foo, bar)` and the subscript syntax `foo[bar]` doesn't.  
> **username_1**: @MarionNewlevant nice... linked to it.  
> **username_1**: @carlcs yeah, tried several variations using `attribute` and couldn't get anything to work.  

---

