How to list sections

Hi,

For posterity, I am still posting it but I figured it out.

A section isn’t just a directory under content/ as the doc says. If it comes with its own index.md, somehow it won’t be part of .Site.Sections. In the example below, renaming the index.md into _index.md and producing a minimalist list template to just show the content from it did the trick.

I am struggling to do this simple things. I use the following code in my baseof.html.

{{ range .Site.Sections }}
  <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
{{ end }}

And my content folder looks like this:

content
├── about
│      └── index.md
├── posts
│      ├── post1.md
│      ├── post2.md
│      ├── post3.md
│      └── post4.md
└── test
       ├── index.md
       └── something.md

I expect to consistently see 3 links returned on every page of my site: ["about", "posts", "test"] (to simplify I will note it as an array and skip the HTML)

And here is the output I get.

On homepage: ["posts"]

On about/ or test/: ["posts"]

On test/something/: ["posts", "test"]

Can someone help me figure what I am missing? I have tried also to use sectionPagesMenu = main and associated logic and I observe a similar behaviour.

Hi @behaghel, I’ve moved your question into a new topic as the original question was from a few years ago, and many things have changed in Hugo since.

You need to read about the Page Bundles feature in Hugo: https://gohugo.io/content-management/page-bundles/

A directory containing an index.md file indicates to Hugo that it is a “leaf bundle”. Essentially, this is a “single” page, potentially with page resources associated with it. For example:

content
├── about
│      └── index.md

is equivalent to

content
├── about.md

with the difference being that you can bundle other things with the first setup:

content
├── about
│      ├── some-photo.png
│      └── index.md

This has other benefits as well, including Page Resources and Image Processing features.

Adding an _index.md file explicitly tells Hugo that the directory is a Section.

content
├── posts
│      ├── post1.md
│      ├── post2.md
│      ├── post3.md
│      └── post4.md

In this example, Hugo will consider /posts/ to be a Section, because it is a top-level directory under content. If you were to add posts/index.md here, this would again become a Page Bundle, with the postx.md becoming Page-type Resources.

4 Likes

@pointyfar thank you very much! Sorry for the delay in coming back to you. It all makes sense now and I have been able to apply this learning to my project :slight_smile: