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

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Collection of modular children in a module > answer

Started by Amaury Hanser 9 years ago · 7 replies · 1140 views
9 years ago

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

Hello,

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):

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

Maybe using the keyword page confuses grav ?

last edited 06/30/17 by flavio
9 years ago

The original, not working code was (for reference)

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

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 🙂

9 years ago

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.

9 years ago

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?

9 years ago

@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.

9 years ago

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.

9 years ago

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 🙂

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.

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

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!

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 44 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago