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'?


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.