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!
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.
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.