How to iterate over pages and sections in a section

Let’s assume we have a folder structure like this:

blog/
├── post1.md
├── post2.md
└── post3/
     ├── _index.md
     └── post3A.md
     └── post3B.md

Now I would like to list in section blog all pages and sections (that is post1, post2, post3) that are childs of blog.
I don’t want to list the childs of post3 (that is post3A and post3B).

Here, section ‘post3’ functions as a kind of wrapper for a segmented larger post.

When I do

{{ range .Pages }}

in my according html I only get post1 and post2.

If I do

{{ range where .Site.Pages "Section" .Section }}

I get post1, post2, post3, post3A and post3B.

I tried

{{ range where (where .Site.Pages "Section" .Section) "Parent" .CurrentSection }}

but this gives an empty list.

Now I have no other idea. Can you help? Thank you!

Try renaming your blog/post3/_index.md to index.md.

Then, make sure you have a template for single pages, e.g. layouts/_default/single.html

Then you can range pages as normal:

{{ range .Pages }}

See the hugo docs on page bundles for more info.

Thank you for your suggestion!

If I do accordingly, however, {{ range .Pages }} gives post1, post2, post3, post3A, post3B.
This is more than I want (no post3A, post3B).

Do I miss something?

It works for me locally, so it’s hard to say what’s going on with your site without seeing your code.

Can your share your site repo? If not, create a sample repo that reproduces your issue, and share that.

I will setup a sample repo. This will take a while, though.

Just to get this right: when renaming ‘_index.md’ to ‘index.md’, ‘post3A’ becomes a leaf bundle, right?
This then can’t have subsequent pages, am I correct?

This would not be a solution for me: I need ‘post3A’ and ‘post3B’ to be accessible as separate pages, listed in section ‘post3’. Is this compatible with your approach?

No, not exactly. post3 becomes a leaf bundle.

Yes.


For reference, here’s the local site I made to test things out.

Give this site structure:

│   config.toml
├───content
│   └───blog
│       │   post1.md
│       │   post2.md
│       └───post3
│               index.md
│               post3a.md
│               post3b.md
├───layouts
│   │   index.html
│   ├───shortcodes
│   │       page-resource.html
│   └───_default
│           single.html

layouts/_default/single.html:

<html>
  <body>
    <h1>{{ .Title }}</h1>
    {{ .Content }}
  </body>
</html>

layouts/index.html:

<html>
  <body>
    {{- range .Pages }}
      <a href="{{ .RelPermalink }}">{{ .Title }}</a>
      <br>
    {{- end }}
  </body>
</html>

From post3/index.md, you can then reference its page resources (page3a.md and page3b.md) with a shortcode, which lives at layouts/shortcodes/page-resource.html:

{{ $page := .Page.Resources.GetMatch (.Get 0) }}
{{ $page.Content }}

Then in your post3/index.md, use the shortcode:

---
title: "Post3"
---

Post3 content.

{{< page-resource "post3a.md" >}}

{{< page-resource "post3b.md" >}}

Which, for /index.html, will output:

<html>
  <body>
      <a href="/blog/post1/">Post1</a>
      <br>
      <a href="/blog/post2/">Post2</a>
      <br>
      <a href="/blog/post3/">Post3</a>
      <br>
  </body>
</html>

And for /blog/post3/index.html, will output:

<html>
  <body>
    <h1>Post3</h1>
    <p>Post3 content.</p>
    <p>Post3a content.</p>
    <p>Post3b content.</p>
  </body>
</html>
1 Like

Thank you very much for your thorough explanation!

I will dig into this and report back how this works in my case.

Ok, this seems to work fine. Thank you very much!

I have a last question, though. How would you go about listing ‘post3A’ and ‘post3B’ in ‘post3’. The solution you provided inserts the content of ‘post3A’ and ‘post3B’ into ‘post3’. I would like to have a list of links, instead.

{{ range .Pages}} doesn’t seem to work in the template for ‘post3’ (because it’s a leaf bundle?)

This is not possible. Per the page resources docs:

Permalink
The absolute URL to the resource. Resources of type page will have no value.

So, you need to organize your content differently, if you wish to link to other posts in your main post.


Right. That will not work because page3a.md and page3b.md are not under .Pages, they are page resources.

1 Like

Thank you for your clarification and your help. I’ve learned a lot here!

Glad to help, and good luck :+1: