Section specific side menu

Hi,

Given the following content folder structure:

.
├── content
│   ├── section1
│   │   ├── overview1.md
│   │   ├── about1.md
│   │   ├── contact1.md
│   ├── section2
│   │   ├── overview2.md
│   ├── section3
│   │   ├── overview3.md

what I’m trying to achieve is that when I navigate to /section1/overview1 for example, a side menu will display all the pages of that section, so overview1, about1 and contact1 in this case.

In my config.toml file I have configured a multi menu menu = ["section1", "section2", "section3"] and each content .md file has the menu configuration in its metadata. So for example, overview1.md has the following front matter:

menu:
    section1:
        identifier: overview1
        weight: 1

My sidebar template looks something like this:

{{ $crrPage := . }}
{{ $crrSection := .Section }}

{{ range (index .Site.Menus $crrSection) }}
    {{ if .HasChildren }}
        <ul class="nav" id="section-list">
        {{ range .Children }}
            <li class="{{if $crrPage.IsMenuCurrent $crrSection . }} active{{end}}"><a href="{{.URL}}">{{ .Name }}</a></li>
        {{ end }}
        </ul>
    {{end}}
{{end}}

I would aspect this to work, but I am new to Hugo so maybe I’m overlooking something or perhaps it’s more of a conceptual error.

The error I get says: ....at <$crrSection>: range can't iterate over <nil>

{{ range (index .Site.Menus "section1") }} or {{ range .Site.Menus.section1 }} work as expected, but anything else I have tried fails with the same error :frowning:

I’ve looked long and hard everywhere. I found this post which basically describes the same use case, the same solution, which in their case works as expected: [SOLVED - solution posted] How do I put .Site.Menu specific to a section in page template

I’m currently on version v0.54.0.

If anyone can point me in the right direction I would much appreciate it. I’ve been stuck with this for a while now and it’s very frustrating.

Thank you <3

Per this error message

....at <$crrSection>: range can't iterate over <nil>

Your .Section is nil. So you’ll need to add logic that only ranges through your side menu if your .Section is not nil

1 Like

Thank you very much for your answer <3

turns out $crrSection was not nil but that index .Site.Menus $crrSection returned nil for a particular $crrSection value, due to missing menu metadata in the files corresponding to that section :expressionless:

Thank you again! I can’t tell you how much I appreciate the help
:sunflower:

1 Like