0

I'd like to store the iteration value to use in data-target so it would be #question0, #quesion1, #question2 and so on...

I tried using {{item.id}} but this didn't work.

{% for item in post.get_field('qanda') %}
<div class="panel panel-default">
    <div class="panel-heading accordion-toggle question-toggle collapsed"
    data-parent="#faqAccordion" data-target="#question{{item.id}}"
    data-toggle="collapse">
        <h4 class="panel-title"><a class="ing">Q:
        {{item.question}}</a></h4>
    </div>
    <div class="panel-collapse collapse" id="question{{item.id}}" style=
    "height: 0px;">
        <div class="panel-body">
            {{item.answer}}
        </div>
    </div>{% endfor %}
</div>
michaelmcgurk
  • 6,367
  • 23
  • 94
  • 190
  • Resolved using answer here: http://stackoverflow.com/questions/10299202/twig-for-loop-and-array-with-key Will post full Answer shortly. – michaelmcgurk Oct 04 '16 at 11:13

1 Answers1

3

You can use the variable loop.index0 (for zero based indexing).

data-target="#question{{loop.index0}}" 

should do what you want. See the for loop documentation for more info for more information.

Jeroen
  • 3,076
  • 1
  • 17
  • 16