Is there a tuple data structure in Twig? If there is, how do you create one?


None that I'm aware of. Since Twig ultimately gets compiled down to PHP and PHP doesn't have first-class citizen support for tuples, you're limited there. You can quasi-fake them with arrays and the list function, but I don't think there is a Twig equivalent for list.

|improve this answer|||||

No tuples per se. You can use arrays:

{% set a = ['a', 'b'] %}
{{ a[0] }}

You can use objects:

{% set o = {'first': 'a', 'second': 'b'} %}
{{ o.first }} or {{ attribute(o, 'first') }}

merge is very useful for constructing arrays and objects:

{% set a = a | merge(['c']) %}
{% set o = o | merge({'third': 'c'}) %}
|improve this answer|||||

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.