Select menus based on directory structure

What’s the correct way to print the menus from hugo.yaml? Let me explain what I try to achieve. My goal is to select menus based on directory structure. Looking at the current repository, /docs/hugo.yaml has a set of menus, while module /project/hugo.yaml has an additional set of menus and following mounts:

module:
  mounts:
    - source: content
      target: content/project
    - source: layouts
      target: layouts/project
    - source: public
      target: public/project
    - source: static
      target: static/project

Which creates the module pages inside /docs/project. My goal is to load a different set of menus, when I’m building /docs/project. Since I defined sectionPagesMenu: project, I expect the navbar to pick the correct set of menus. I have the following condition into navbar:

{{- $menu := .Site.Params.sectionPagesMenu | default "main" -}}

Running locally the server, to generate the site:

hugo server --disableFastRender --logLevel debug -Ds ./docs

I’m not sure I understand, but if you know the menu name you can use the index function:


{{ $menu := "something" }}

{{ with index site.Menus $menu }}
  <nav class="menu">
    <ul>
      {{ range . }}
        {{ if $.IsMenuCurrent .Menu . }}
          <li class="active"><a aria-current="page" href="{{ .URL }}">{{ .Name }}</a></li>
        {{ else if $.HasMenuCurrent .Menu . }}
          <li class="ancestor"><a aria-current="true" href="{{ .URL }}">{{ .Name }}</a></li>
        {{ else }}
          <li><a href="{{ .URL }}">{{ .Name }}</a></li>
        {{ end }}
      {{ end }}
    </ul>
  </nav>
{{ end }}

@jmooring thank you for taking the time to look into this, I apologize for late response. I think the easiest way is to clone the repo and run the hugo server, to understand better the issue. I’m doing exactly what you mention above, see related code.

If you run hugo server at directory root:

hugo server --disableFastRender --logLevel debug -Ds ./docs

And visit http://localhost:1313/, the frontpage displays as it should, there are no menus. The problems arise when you visit http://localhost:1313/project/:

In order to achieve the desired results (see live example for /docs and /docs/project), I have to run twice the deployment, which breaks the sitemap and adds a lot of duplicated files into /docs/public, as well /docs/public/project.

I apologize in advance for the lengthy reply. I’ve been trying to fix this issue for over a week, spending many hours, without any progress.

CC @razon @irkode for visibility.

In your config, sectionPagesMenu isn’t under the params key. Why?

I apologize, I’m very new at Hugo, I was looking at the documentation and it was not specified. I’ll move it under params. The funny part is that I had the $menu coded correctly, novice mistake.

I was reading somewhere into documentation that the layouts partials are interpreted differently, so I thought is related to that. I’m going to change the code later and report back, thank you!

Disregard my previous comment. I was thinking of mainSections, not sectionsPagesMenu.

Just making sure, is that the correct format?

{{- $menu := .Site.Params.sectionPagesMenu | default "main" -}}

Or it should be .Site.SectionPagesMenu first letter upper camel case, like into source code.

I cannot find absolutely nothing on the Internet.

Edit: Netlify to the rescue, the correct value is either .Site.Params.SectionPagesMenu or .Site.Params.SectionPagesMenu. I’m going to use the first choice, for Uniformity with other defined variables.

That value is not exposed to the templates.

If it were, it would be listed here:
https://gohugo.io/methods/site/

1 Like

That explains why it works. Then there is no real fix to replace the hardcoded main value, this is what the developer used:

{{- range .Site.Menus.main -}}

I wanted to have the choice to replace the main with project into template. I’m going to close the PR, is useless.

@jmooring I reverted back the menus to main. I’m trying to understand what condition I need to implement, so Hugo can see the new navbar menus present into /projects/hugo.yaml module, at build time.

The menus are displayed correctly into sidebar, so the configuration variables are properly read.