<p>Not sure if you saw this, but you may also be able to use the Twig <code>attribute</code> function:</p>

<pre><code>{{ attribute(array, item) }}
</code></pre>

<p>Pretty straightforward, here's <a href="http://twig.sensiolabs.org/doc/functions/attribute.html" rel="noreferrer">the official docs page...</a></p>

CC BY-SA 3.0
2014-07-03T03:52:52.723Z
Thanks. it was the property being dynamic too which was causing me to have all sorts of issues.
Iain Urquhart
CC BY-SA 4.0
2019-03-20T16:56:59.000Z
Noting here an application of this function because it was not 'straightforward' to me until I hit upon the solution by trial and error! In my case the variable is the **Globals field handle**, so I've ended up with `{{ attribute(globalSetHandle, globalsFieldVariable') }}`, which works a treat.
Jonathan Schofield
CC BY-SA 3.0
2014-07-03T03:38:00.220Z
2014-07-03T03:38:00.220Z
Lindsey D
<p>I've got a bit of a workaround, not sure if it's the best way to do it.</p>

<p>I restructured my fields, turns I don't need unique field names for the globals (still thinking in EE mode) so that took away half the problem.</p>

<p>I can access the global group dynamically, using <a href="http://twig.sensiolabs.org/doc/templates.html#global-variables" rel="nofollow">_context</a></p>

<p>So after renaming my Video ID field, I can call the global set dynamically using:</p>

<pre><code>{{ _context[countryCode~'_Variables'].vimeoId }}
</code></pre>

CC BY-SA 3.0
2014-07-03T03:29:28.123Z
2014-07-03T08:21:14.567Z
2014-07-03T08:21:14.567Z
Iain Urquhart
<p>I've got a parent template which contains the following code:</p>

<pre><code>{% include '_layout' with {'countryCode': 'au'} %}
</code></pre>

<p>I have a global set with the handle <code>au_Variables</code>, and within that a field with the handle <code>vimeoVideoId_au</code></p>

<p>The problem I'm having is with trying to render the following global: <code>{{ au_Variables.vimeoVideoId_au }}</code> dynamically, using the countryCode.</p>

<p>I've tried </p>

<pre><code>{% set videoId = countryCode~'_Variables.vimeoVideoId_'~countryCode %}
</code></pre>

<p>And then outputting <code>{{ videoId }}</code> but all I get is the string <code>au_Variables.vimeoVideoId_au</code>, not the rendered global.</p>

<p>Any ideas how to do this in Twig?</p>

<templating><variable><concatenation>
Calling variables, dynamically
# Calling variables, dynamically

(Asked by: username_0 on 2014-07-03)

I've got a parent template which contains the following code:
{% include '_layout' with {'countryCode': 'au'} %}

I have a global set with the handle au_Variables, and within that a field with the handle vimeoVideoId_au
The problem I'm having is with trying to render the following global: {{ au_Variables.vimeoVideoId_au }} dynamically, using the countryCode.
I've tried 
{% set videoId = countryCode~'_Variables.vimeoVideoId_'~countryCode %}

And then outputting {{ videoId }} but all I get is the string au_Variables.vimeoVideoId_au, not the rendered global.
Any ideas how to do this in Twig?

**Tags:** templating, variable, concatenation


---

## 2 answers

### Answer by username_0 on 2014-07-03. Score: 4 | Accepted answer

I've got a bit of a workaround, not sure if it's the best way to do it.
I restructured my fields, turns I don't need unique field names for the globals (still thinking in EE mode) so that took away half the problem.
I can access the global group dynamically, using _context
So after renaming my Video ID field, I can call the global set dynamically using:
{{ _context[countryCode~'_Variables'].vimeoId }}


---

### Answer by username_1 on 2014-07-03. Score: 5

Not sure if you saw this, but you may also be able to use the Twig attribute function:
{{ attribute(array, item) }}

Pretty straightforward, here's the official docs page...

> **Comments:**
> **username_0**: Thanks. it was the property being dynamic too which was causing me to have all sorts of issues.  
> **username_2**: Noting here an application of this function because it was not 'straightforward' to me until I hit upon the solution by trial and error! In my case the variable is the **Globals field handle**, so I've ended up with `{{ attribute(globalSetHandle, globalsFieldVariable') }}`, which works a treat.  

---

