[SOLVED] Nested sections from main section in v0.22

I’m having trouble understanding how to use nested section (tree) navigation. I have consulted the demo site but nothing seemed to render for me.

Specifically, I wanted to crawl and display pages by subsection from a specific main section — in my case labeled “patterns”. I discovered the group-by-section feature and thought I could use this. So far, I have been able to use it to filter out the other (main) sections and remove non-regular pages, like so:

{{ range .Site.RegularPages.GroupBy "Section" }}
  {{ if eq .Key "patterns" }}
    <h2>{{ .Key }}</h2>
    <ul>
      {{ range .Pages.ByTitle }}
        <li>
          <a href="{{ .Permalink }}">{{ .Title }}</a>
        </li>
      {{ end }}
    </ul>
  {{ end }}
{{ end }}

However, I’d like to use the naming mechanism (.Key) to separate top level subsections within this “patterns” section. Something like:

{{ range .Site.RegularPages.GroupBy "Section" }}
  {{ if eq .Key "patterns" }}
    <ul>
      {{ range .Sections }}
        <li> {{ .Key }}
          <ul>
            {{ range .Pages.ByTitle }}
              <li><a href="{{ .Permalink }}">{{ .Title }}</a></li>
            {{ end }}
          </ul>
        </li>
      {{ end }}
    </ul>
  {{ end }}
{{ end }}

I don’t need to tell you that the nested range .Sections part doesn’t work, but I’m wondering what would? I suspect there is a way using the new nested sections feature but, as I said, I couldn’t get that to work for me. Many thanks and congratulations on the new release!

Why does that not work?

I think you need to show the full non-working site for me to be able to understand what you’re trying to do.

1 Like

Here’s the code from the site:

Under /content/ I have

Patterns
— Landmarks
—— example1.md
—— example2.md
— Widgets
—— example1.md
—— example2.md

I’m trying to create a nested list navigation (like a table of contents) of all of this. I get a prompt about deprecation in the console which suggests using the following for sections:

{{ range $.Site.Home.Sections }}
  Section: {{ .Title }}
  {{ range .Pages }}
     Section Page: {{ .Title }}
  {{ end }}
{{ end }}

Great. So I think I should be able to nest this like so:

{{ range $.Site.Home.Sections }}
  Section: {{ .Title }}
  {{ range .Pages }}
     Section Page: {{ .Title }}
  {{ end }}
  {{ range .Sections }}
     Subsection: {{ .Title }}
     {{ range .Pages }}
       Subsection page: {{ .Title }}
     {{ end }}
  {{ end }}
{{ end }}

But the subsection content simply doesn’t render. So it looks like nested sections are not iterable at all. I can verify I’m using Hugo Static Site Generator v0.22.1 darwin/amd64 BuildDate: 2017-06-14T22:34:05+08:00.

There is one rule that needs to be followed re. nested sections: The bottom sections (in your case Landmarks, Widgets) needs _index.md – all sections can have a content file, but those sections have to have one.

3 Likes

Thanks for the help. So, it looks like I can get that working if I have _index.md files for each, like you said!

But is there any way of accessing the section name/title by the folder name? I’m creating a content system for the non-technical and this would mean less work/confusion for them. I guess I just thought that info would be available via Go’s crawling of the file system already.

Thanks again for the pointer.

Yes there is.

Try:

{{ $landmarks := .Site.GetPage "section" "Patterns" "Landmarks" }}

Or:

{{ $landmarks := .Site.GetPage "section" "Patterns/Landmarks" }}
4 Likes

That only returns a .Title if the _index.md includes one. Is there no way of looking up to the folder and taking its name?

I suggest you go read the documentation, esp. the part about the Page variables etc.