Calling a variable with a dynamic name (aka how to make a navigation menu different per section using front matter)

I am trying to use a menu which is different per section. I would like to run something similar to:

{{ range .Site.Menus.$currentSection }}

but it doesn’t obviously work in this. I’ve tried several ways but always get an error. Which should be the way to make it work?

thanks in advance


Do you want:

  1. a menu where one point with submenu changes per section

  2. each section to have it’s own menu


Because 2) would be easy. You “just” create multiple menues and then in the template where it’s created you check which section you are in and use that menu.

In either case, you might want to post a structure example from section a and section b so we can understand what you want to make different.

Each section has to have a different menu which I want to configure via front matter.
As I want to total flexibility and reusability, I am avoiding any ‘static’ entry into to template.
section/list.html is common for any section.
Based on section name, you pull out the menu from config.
Beyond the approach, the question is more general: can I make something like

{{ $myvar := .Section }}
{{ if .Site.Something.$myvar ..... }} 

I guess it’s more a ‘go’ matter on how to compose the structure.


Try the index function:

something like {{ index .Site.Sections .Site.Section }}? (tried this one, no go)

@bronze you’re right, it doesn’t work in that way :grinning:
@zwbetz if I understand right, the index wants to have a config file in /data/something/.

I’ve doing some test and researches, let me try to reformulate.
I want to make a site where there’s a navigation menu which is contextual per section. As I want this to be fully configuration, I’m looking to make it work using the front matter, rather than coding something static in the template.
Really, the navigation menu should just display pages available in the section itself. A workaround approach, (which I’ve tried but I am sure can work) is described here: SOLVED: List pages in sub-section
The drawback of this approach is that pages will be listed alphabetically (cause I reckon there’s no way to force an order) and it will display all pages.

The menu template doesn’t allow any .Section parameter so my need to ‘compose’ the call with a variable (.Section).

have u tried the weight: 1 frontmatter config?

edit: what kind of pages u using on the sections? or

Right. But that’s just one example. To use your example:

{{ index .Site.Something $myvar }}

@bronze haven’t tried the weight yet. THe section can be either a branch or leaf. I am sure I do not need any subsection, so I can probably use just

@zwbetz right, have to try. I understand here it’s a go stuff and not hugo; however the manual might be more specific.

@LunaticMuch I don’t think we’re on the same page. This snippet from the index docs may help add clarification:

In Go templates, you can’t access array, slice, or map elements directly the same way you would in Go. For example, $.Site.Data.authors[.Params.authorkey] isn’t supported syntax.

Instead, you have to use index , a function that handles the lookup for you.

@zwbetz I’ve tried, no luck. I can obviously make two files:


with a menu syntax, but it doesn’t get inherited as a menu.
I’ve tried to render a variable with a dynamic name, which I think it’s the problem. If I use a syntax like:

{{ range (print "site.Menus." .Section) }}
{{ end }}

the print function works, and make the right stuff, but Hugo generates a build error as:

Error: Error building site: failed to render pages: render of "section" failed: "/Users/LM/SITES/demo/themes/hugo-flex/layouts/_default/list.html:11:23": execute of template failed: template: _default/list.html:11:23: executing "main" at <"var">: range can't iterate over Site.Menus.section1

Apparently, the engine is able to resolve the variable, but hangs somewhere else.
If I type statically {{ range site.Menus.section1 }}, this works as expected.

It’s hard to help further without seeing your code. But using your example, with index, would look like:

{{ range (index site.Menus "section1") }}