Group results of nested loop

I have a nested loop like this:

{% for group in page.children %}
   {% for item in group.children.order('title', 'desc') %}
      {{ loop.index }} 
   {% endfor %}
{% endfor %}

I am wanting to group all “item” results as a whole, so that the “loop.index” doesn’t reset when it’s a new “group”. Also, I am wanting all of the “item” results to be ordered by title as a whole. I haven’t been able to accomplish this after many attempts. Thanks in advance for your help.

You are going to need to assign the loop.index to another local variable inside the first for loop.

Thanks so much! I’m not finding any Twig docs on how to do this. Does anyone have further info?

The Twig docs for it are here: http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable

Thanks so much! I ended up with a solution by doing this:

{% set item_count = 0 %}
{% for group in page.children %}
   {% for item in group.children.order('title', 'desc') %}
      {% set item_count = item_count + 1 %}
   {% endfor %}
{% endfor %}
---
1 Like