Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-22T08:06:10.323Z
Answers.Text: tried it that way, but i get this in response:
  Impossible to access an attribute ("globalAddress") on a string variable ("fr_foobar")
Answers.UserDisplayName: Stefan Friedrich
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-22T18:31:24.893Z
Answers.Text: Updated the answer and actually tested this one. :)
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T19:48:27.480Z
Answers.Text: `_context` documentation, such as it is, is here: http://twig.sensiolabs.org/doc/tags/macro.html
Answers.UserDisplayName: Marion Newlevant
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T21:45:04.560Z
Answers.Text: @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.
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T22:04:10.460Z
Answers.Text: @MarionNewlevant nice... linked to it.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-26T22:04:29.503Z
Answers.Text: @carlcs yeah, tried several variations using `attribute` and couldn't get anything to work.
Answers.UserDisplayName: Brad Bell
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-21T19:23:28.783Z
Answers.LastActivityDate: 2016-01-26T22:03:47.177Z
Answers.LastEditDate: 2016-01-26T22:03:47.177Z
Answers.UserDisplayName: Brad Bell
Body: <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>
  
Tags: <templating><php><variable><set>
Title: Variable variables in Twig
ThreadText: # 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](http://twig.sensiolabs.org/doc/tags/macro.html) `_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**: @username_1 ...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**: @username_2 nice... linked to it.  
  > **username_1**: @username_3 yeah, tried several variations using `attribute` and couldn't get anything to work.  
  
  ---
  
  