Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

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 (en.wikipedia.org/wiki/%3F:)? –  Brad Bell Jun 27 at 8:37
    
@Brad I mean tuple (first :) ) –  nicael Jun 27 at 8:38

2 Answers 2

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

 
discard

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.