Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
Do you mean tuple or ternary operator ( – Brad Bell Jun 27 '14 at 8:37
@Brad I mean tuple (first :) ) – nicael Jun 27 '14 at 8:38
up vote 5 down vote accepted

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.

share|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'}) %}
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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