3

I am working on a testimonial slider and each slide is wrapped in an <li> tag. The JS looks for a similar id number, one for each layer of the slide, in this case I need to change the 1 in the example below.

Example:

"rs-1" id="slide-1-layer-2" id="slide-1-layer-3"

I loop through the entries with a normal {% for %}{% endfor %} statement and I tried rendering the required number (1) with the {{ loop.index }} but I guess you can only use this once...

Is there a method do this? Perhaps with Twig?

If I am not clear this is what the result should be:

<li>"rs-1"
        id="slide-1-layer-2" 
        id="slide-1-layer-3" 
</li>
<li>"rs-2"
        id="slide-2-layer-2" 
        id="slide-2-layer-3" 
</li>
<li>"rs-3"
        id="slide-3-layer-2" 
        id="slide-3-layer-3" 
</li>

etc...

2
  • Nutmeg, I think the answer below is correct or do you still have issues with something?
    – Jan_dh
    Oct 5, 2016 at 13:14
  • yes, thank you Jan! Next is to use this in a sum, but i am sure thats done with a Twig native tags
    – Nutmeg
    Oct 5, 2016 at 21:21

1 Answer 1

6

Set it as a variable inside your loop and you can reuse it all you want.

 {% for entry in craft.entries.section('yourSection') %}
    {% set loopIndex = loop.index %}
    <li>"rs-{{loopIndex}}" id="slide-{{loopIndex}}-layer-2"</li>
 {% endfor %}

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.