Twig batch and loop index

Viewed 4287

I have a Twig for loop using the batch filter to wrap every 2 elements in a container div. I want to add a classname to every 3rd and 4th div in this for loop. However it seems you can't use loop.index when using the batch filter. Is that correct? How do you access the index then when using the batch filter?

What I tried is this:

 {% for batch in blog.articles | limit(6) | batch(2) %}
    <div class="blog-art-wrap row-eq-height">
    {% for article in batch %}
      <div class="article {% if loop.index == 3 or loop.index == 4 %}some-class{% endif %}">...... </div>
    {% endfor %}
  </div>
 {% endfor %}

I also tried it with loop.index3 etc... But it just seems to ignore this. Or is this because of the batches are in 2? So there's actually no index 3 and 4?? If so how do you access every 2nd batch then?

2 Answers
Related