Iterate current menu

I’ve got a couple menus on my site, called “usage” and “core”. I’d like for my nav to display the current menu—that is, the menu that the current page is in.

Based on the example on the Menu Templates page, I can hard-code one menu using {{range .Site.Menus.usage}}. I also know that .Params.menu equals “usage” or “core”, depending on the page. Is there a way to do something similar to {{range .Site.Menus[.Params.menu]}} so I can use the same nav partial for any page?

I think you can do something like {{ range (printf ".Site.Menus.%s" $var) . }}

1 Like

That did it. Thank you!

For anyone else who finds this useful, I wrapped the whole thing in a conditional: {{if in .Site.Menus .Params.menu }} to filter out pages that aren’t in a menu.

edit Actually, it looks like that filter didn’t work…

1 Like

Any idea how I can filter out pages that don’t have a menu param? They fail to render

range can’t iterate over .Site.Menus.%!s(<nil>)

That one beats me …

Maybe intersect pages with .Params.Menu, save it into a variable and apply the range to it? …

Did anyone eventually figure out a solution to this? Cause I’m struggling with the same thing and nothing I’ve tried seems to work :frowning: