<p>There may be other ways also, but one approach is to use an array with key value pairs, which can be assigned and accessed more dynamically.</p>

<pre><code>{{ sizeVars[imgSize ~ 'x2'] }}
</code></pre>

<p>You also might want to check out twigs <code>attribute</code> method.</p>

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

<p>Where 'item' can be a concatenated string.</p>

CC BY-SA 3.0
2015-04-12T15:35:35.693Z
2015-04-12T15:35:35.693Z
Douglas McDonald
<p>I’m trying to append a 'x2' to the end of a variable name to call another variable. </p>

<p>For example:</p>

<p>imgSize might be set to predefined variables like imgColumn4, imgColumn6 or imgColumn8.
In that case I would also have imgColumn4x2, imgColumn6x2 and imgColumn8x2.</p>

<p>So to simplify the variables in a loop, I’d like to do something like:</p>

<pre><code>{% set imageUrl = image.getUrl(imgSize) %}
{% set imageUrlx2 = image.getUrl(imgSize ~ 'x2') %}
</code></pre>

<p><code>imgSize ~ 'x2'</code> does not work.</p>

<p>How can I join 'imgSize' and 'x2'?</p>

<templating><variable><set>
How to extend a variable name to fetch another?
# How to extend a variable name to fetch another?

(Asked by: username_0 on 2015-04-12)

I’m trying to append a 'x2' to the end of a variable name to call another variable. 
For example:
imgSize might be set to predefined variables like imgColumn4, imgColumn6 or imgColumn8.
In that case I would also have imgColumn4x2, imgColumn6x2 and imgColumn8x2.
So to simplify the variables in a loop, I’d like to do something like:
{% set imageUrl = image.getUrl(imgSize) %}
{% set imageUrlx2 = image.getUrl(imgSize ~ 'x2') %}

imgSize ~ 'x2' does not work.
How can I join 'imgSize' and 'x2'?

**Tags:** templating, variable, set


---

## 1 answers

### Answer by username_1 on 2015-04-12. Score: 3 | Accepted answer

There may be other ways also, but one approach is to use an array with key value pairs, which can be assigned and accessed more dynamically.
{{ sizeVars[imgSize ~ 'x2'] }}

You also might want to check out twigs attribute method.
{{ attribute(array, item) }}

Where 'item' can be a concatenated string.


---

