Show menu in sidebar depending on page

I have information pages on my site, which are grouped by service. Let’s say I have service A and B, with matching menus. Pages about service A have the servicea menu specified, and those about service B have the menu serviceb.

I’d like to display a sidebar menu conditionally. If the current page has menu “servicea” set in its frontmatter, then I’d like to show the corresponding menu, and vice versa for “serviceb”. Conversely, I don’t want to show both menus.

The following works to show the “servicea” menu, but, it appears for all inner pages pages, including those pages with only “serviceb” set.

{{- $currentPage := . -}}
  {{ range .Site.Menus.servicea }}
     <a href="{{ .URL | absLangURL }}"><p class="menu-label">{{ .Name }}</p></a>
         <ul class="menu-list">
          {{ if .HasChildren }}
          {{ range .Children }}
            <li><a class="is-hoverable{{ if $currentPage.IsMenuCurrent "servicea" . }} is-active{{ end }}" href="{{ .URL | absLangURL }}">{{ .Name }}</a></li>
          {{ end }}
          {{ end }}
       {{ end }}

I can see that {{ $currentPage.Menus }} is a map but, I cannot use “in” on it.

Can anyone tell me, how can I test which menu the current page has, then show the appropriate menu for it?

If I understand correctly I would do it this way:

{{ with .Params.whatmenu }}
  {{ range ( index .Site.Menus . ) }}
    {{ $currentPage = $ }}
  {{ end }}
{{ end }}

Thanks @regis. I tried it, but it fails.

When I add {{ }} to my single template, I can see the map of the menus on the page. With a page with only the one menu, I see:

    map[service:map[name:Helpdesk weight:10 parent:osoutsourcing_en identifier:oshelpdesk_en url:/helpdesk]]

I can also specify the specific menu via {{ }} and get the filtered map, like:

    map[name:Helpdesk weight:10 parent:osoutsourcing_en identifier:oshelpdesk_en url:/helpdesk]

When I try passing that to with, it fails.

     {{ with }}

       {{- $currentPage := . -}}
       {{ range .Site.Menus.outsourcing }}

       {{ end }}

Neither with nor with returns anything.

When I tried making it exactly like what you said, first I got an error about the =, so I tried changing to :=. Then it complained about the index, so, I started looking into what is being seen, step by step.

Now I am not so sure how to proceed.

I can probably do this with page type, and just make different singles that call different sidebar partials.

Should be:

{{- $currentPage := $ -}}

But I guess you’ve played a lot with it to no avail. If only I had a repo :slight_smile:

Hi @regis, it’s very much a messy “work in progress” so it’s embarrassing to share, but, thank you! The repo is here:

I guess the relevant bits are as follows. I’m starting hugo server like this:

hugo server --navigateToChanged --buildDrafts --watch --verbose --source="/path/to/eSolia_2018" --config="/path/to/eSolia_2018/config.toml" --port=1366

I’ve been editing the sidebar menu here in the single:

I’ve been testing on this set of content pages:

Those have the menus set up, where outsourcing is the parent of helpdesk.

Two more pages are outside that outsourcing section, so I’d like to use a different menu, this time “consulting”:

The idea is, the outsourcing set should show the outsourcing menu. The consulting set should show the consulting menu.

My hugo env is:

Hugo Static Site Generator v0.39-DEV-4F639D6B darwin/amd64 BuildDate: 2018-04-02T22:30:36+0900

Thank you in advance. :slight_smile:
Today was an early day and it’s late (in Japan) so I’ll get some sleep now.

Damn! I got fooled by the dot! Again!
.Site cannot work within that range, should be $.Site.menus

The following worked on your repo (with my own environment, I must admit I didn’t play with yours)

in your Front Matter add:

whatmenu: outsourcing

In your single:

{{- $currentPage := . -}}
{{ with .Params.whatmenu }}
   {{ range (index $.Site.Menus .) }}
   {{ end }}
{{ end }}
1 Like

Thanks @regis! I’ll work with that!