Iterate over sibling pages in child section

Directory structure:

content
    systems
        sys1.md
        sys1
            a1.md
            b1.md
            c1.md
        sys2.md
        sys2
            a2.md
            b2.md
            c2.md

In the layout that applies to a1.md, I would like to iterate over all of its sibling pages, i.e. b1 and c1 and only those. It can return a1, too.

I tried:

{{ range .Page.Parent.Pages }}

but that .Page.Parent seems to refer to the systems section, and gives me far too much. How do I do this?

content/systems/sys1/ is not a Section. So the Parent of a1 is /systems/.

I would suggest a restructure:

β”œβ”€β”€ sys1
β”‚   β”œβ”€β”€ _index.md   # sys1.md
β”‚   β”œβ”€β”€ 1a.md
β”‚   └── 1b.md
└── sys2
    β”œβ”€β”€ _index.md   # sys2.md
    β”œβ”€β”€ 2a.md
    └── 2b.md

I could do that if there is no other way, but I’d rather not as the URL namespace is important to me.

If by URL you mean the rendered paths, I’m not sure I understand: systems/sys1/_index.md and systems/sys1.md will both be rendered to yoursite.com/systems/sys1/ (assuming default configs elsewhere) so that URL will not change.

If you really do not want to do that, you could perhaps play around with some .File.Path filtering.

This is possible with the .CurrentSection variable, which would give access for a1.md to all pages in /systems/sys1/. There’s more information here: section page variables and methods.

This does require that you use _index.md files. Even though you don’t, Hugo requires them. I don’t know of an option to work around that.

Ha! It’s working. I hadn’t understood that _index.md defines a section, apparently, and that .CurrentSection.Pages contains the pages of the current section. Thanks, everybody.

2 Likes