Automatic nested menu listing all sections, subsections and pages

I’ve googled quite some time to find out how to “automatically” create a menu listing all sections, n-level subsections and pages (see Lazy blogger menu (weird suffix, nested)).

Sadly, the “lazy blogger menu” does not work for my case, since it lists only the top level sections and neither subsections nor pages.

So this is what I’ve come up with:

{{ $page := .currentPage}}
{{ range .menu }}
        <a href="{{ .URL }}" class="{{if .IsAncestor $page}}active{{end}}">{{ .Name }}</a></li>
        {{ if .Sections }}
            {{- partial "section-menu.html" (dict "menu" .Sections "currentPage" $page)}}
        {{ end }}
        {{ $sectionPages := where .Pages "File.Dir" "in" .Dir }}
        {{ range $sectionPages }}
                <a href="{{ .URL }}" class="{{if eq . $page}}active{{end}}">{{ .Name }}</a></li>
        {{ end }}
{{ end }}

Use it like this for listing all sections of the site:

{{- partial "section-menu.html" (dict "menu" .Site.Sections "currentPage" .Page) -}}

Or like this to list only the subsections and subpages of the current section:

{{- partial "section-menu.html" (dict "menu" .CurrentSection.Sections "currentPage" .Page) -}}
1 Like

Thanks for the tip. I think you will also like this one