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

Access Childpage Index in Collection Loop

Started by Muut Archive 10 years ago · 2 replies · 1302 views
10 years ago

If I'm looping through a collection of modular pages, how can I access the index of the child page in the collection array from the modular template?

The loop in page.html.twig

TWIG
    {% for childpage in page.children %}
        {{ childpage.content }}
    {% endfor %}

And the childpage modular template in modular/childpage.html.twig:

TWIG
{{ content }}
<!-- this is where I need the current index in the collection array -->

I know without context of the markup around the modular template it seems redundant, but I just needed to establish the bare-bones setup.

10 years ago

Yes, the loop.index is what I'm looking for, but my question in this context is how to pass the loop.index as a var to the modular template — does that make sense?

So something like:

TWIG
{% for childpage in page.children %}
        {% set childIndex = loop.index %}
        {{ childpage.content }}
    {% endfor %}

And then in the modular/childpage.html.twig template:

TWIG
 {{ childIndex }}

(which doesn't work)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1357 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 2955 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago