List all pages in a section on every page in that section

I’m converting our docs site from Jekyll to Hugo and I’m having trouble figuring out how to implement our multi-page documents like this one.

These documents need to list all pages in the document in an order. And we need to have next/previous links as well. I have tried to use Sections and weight frontmatter, but so far have run into trouble with nested sections.

I have the following document structure:

content/
  lorem/
    _index.md
    ipsum.md
  dev/
    _index.md
    dolor/
      _index.md
      sit.md
    amet/
      _index.md

In the lorem document, I can successfully use the following to get a list of pages in the lorem section for lorem/ and /lorem/ipsum/.

<ul>
		{{ range where .Site.RegularPages "Section" .Section }}
			<li>
				<a href="{{ .Permalink }}">{{ .Title }}</a>
			</li>
		{{ end }}
</ul>

However, this does not work in the /dev/dolor document. It seems to return all pages in /dev. And when I print the .Section in the template it is always dev. The same is true when I try .CurrentSection.

Perhaps I’ve misunderstood nested sections, but reading the docs it seems to me that when looking at a page at /dev/dolor/_index.md the section should be dolor. What have I missed?

(Also, I’d be happy to take alternate suggestions for how to implement this. Sections seemed the best way to generate a table of contents as well as be able to use previous/next links.)

It’s not totally clear what you want, but the .Section string always points to the “root section”.

If you from the dev section want to list all pages “below” you probably want to drop the where clause and just just use .RegularPagesRecursive (added in a recent Hugo version).

For prev/next to follow that scheme, see https://gohugo.io/variables/pages/#prev-page

1 Like

Thanks for your respond, @bep! I’ve tried using .RegularPagesRecursive but this only works on dev/dolor. When I visit dev/dolor/sit there are no pages displayed.

I’ll try to be clearer about what I need, because it seems like maybe I am missing something conceptually about how Hugo defines relations between pages.

Our documentation website includes dozens of different guides. Each of these guides has more than one page, and I need something like a Table of Contents that displays each page in a guide as sequential “chapters” in a “book”.

- Introduction
- Chapter 1
- Chapter 2
- Chapter 3

No matter which chapter I’m viewing, I want to be able to see this list so that I can currently see I’m reading Chapter 2 of the book and there are other chapters to view. My current approach is to try to treat each “book” as a Section in Hugo:

content/
  book-one/
    _index.md
    chapter-1.md
    chapter-2.md
    chapter-3.md
  book-two/
    _index.md
    chapter-1.md
    chapter-2.md

The documentation suggests that I should be able to use this structure along with nested sections to relate chapters together into a book, even in nested directories. But maybe I’m misunderstanding something there.

This is what my real directory structure looks like:

content/
  book-one/
    _index.md
    chapter-1.md
    chapter-2.md
    chapter-3.md
  book-two/
    _index.md
    chapter-1.md
    chapter-2.md
	dev/            <-- All technical documentation
	  tech-book-one/
		  _index.md
			chapter-1.md
			chapter-2.md
			chapter-3.md
	  tech-book-two/
		  _index.md
			chapter-1.md
			chapter-2.md
			chapter-3.md

Am I going about this completely the wrong way? We’re trying to migrate away from Jekyll and I’m open to exploring any solution that works, including a manually maintained Table of Contents data structure (as long as we can support prev/next links).