Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Group results of nested loop

Started by Muut Archive 10 years ago · 4 replies · 462 views
10 years ago

I have a nested loop like this:

TWIG
{% 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.

10 years ago

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

10 years ago

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

10 years ago

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

TWIG

{% 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

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2957 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago