Collection of modular children in a module > answer

Continuing the discussion from Create a collection of modular children in a module?:


If what you want is to have a menu in your modular page with links to the different submodular page, it’s possible.
This works (just tested):

    <ul class="list-inline">              
       {% for module in page.collection %}
          <li class="sqr_button">
          	<a href="#{{ module.slug }}">
                {{ module.slug}}
       {% endfor %}

Maybe using the keyword page confuses grav ?

The original, not working code was (for reference)

<ul class="list-inline">              
   {% for page in page.collection %}
   {% endfor %}

If just changing the for variable from module to page makes it work, it might as well be a case where page is overwritten in the loop and this causes confusion for Twig.

Always better not to overwrite variables in that way :slight_smile:

There are some usecases where you actually do want to override the page variable. For example if you use a shared partials twig template that expects page. Just FYI.

I thought I understood collections in Grav but I’m still struggling with this.
I just can’t seem to get any output when I’m in a child page of a modular page. Anybody have any suggestions?

@csixtyfour Where and how are you defining your collection? And then where and how are you trying to use it? Your not the original poster, so we would need more info for your use case.

Sorry for the confusion, the original post from the old system that this new post is in response to was actually mine - and reminded me this was still TODO.

I’m thinking this isn’t possible the way I’m imagining it but my basic question is:
I’ve got a modular page with say 5 children within it. In the first child section I’m trying to create a menu of the other children in the page but I can’t seem to figure out how to iterate through the rest of the children in the module.

I’m thinking now I can only create this collection at the modular page level and need to figure out the best way to import this menu into my child page - possibly via a macro.

Sorry for the confusion, i know it’s going to be a little tricky for a few weeks until we’re all comfortable on the new forum :slight_smile:

I think the best approach is from the modular twig, get the parent, then the children and iterate over those, skipping the current page. Something like this where the page is actually the instance of the first modular page.

{% for modular_page in page.parent.children if modular_page.route != page.route %}
<li>{{ modular_page.title }}</li>
{% endfor %}

Ah yes that’s much better approach and makes more sense than trying to import from the page level. I didn’t even realize a page.parent.children type option was available.
Thanks a lot for your assistance.

Also thanks for moving over to discourse its so much easier to navigate and search!