Access page params inside of menu loop

I have a parameter in the front matter of my pages that should trigger a certain class. Think of a boosted entry that will be displayed different.

How do I access that parameter inside of a menu loop?

Frontmatter of a page:

---
boost: true
---

in the menu partial:

{{ range .Children }}
  <a class="dropdown-item {{ with .Params.boost }}bg-secondary {{ end}}" href="{{ .URL }}">
    {{ .Title }}
  </a>
{{ end }}

I would have expected Hugo to have the params available at this point, but it bails out with:

Failed to render pages: 
render of "section" failed: 
execute of template failed: 
template: _default/list.html:4:12: 
executing "_default/list.html" at <partial "header.html" .>: 
error calling partial: 
execute of template failed: 
template: partials/header.html:1:3: 
executing "partials/header.html" at <partial "header/navigation.html" .>: 
error calling partial: "/.../partials/header/navigation.html:15:57": 
execute of template failed: 
template: partials/header/navigation.html:15:57: 
executing "partials/header/navigation.html" at <.Params.boost>: 
can't evaluate field Params in type *navigation.MenuEntry

Debugging the menu items with debugprint.html (Thank you @kaushalmodi) shows that indeed there are not params in the menu entry.

Are your menu items declared via config? or page frontmatter?

If via frontmatter, you can access the .Page object: https://gohugo.io/variables/menus/#menu-entry-variables

and then get the .Params from it.

Otherwise, I suppose you could check with a .GetPage .URL or something similar, then access the .Params.

1 Like

Perfect, I knew there must have been something like that. Thanks!

I had the idea of going the .GetPage way, but the more pages are in the nav the more non-cachable request I would have had… better clean as above.