How to get menu parameters in a template for a given page?


I have the following use case: The featured image on a documentation page should use the icon from its entry in the sidebar menu.

My current solution is to write the ID of the icon into the page scratch when the menu entry is found while generating the sidebar menu. Later on the page I read the ID from the scratch to get the icon again. This avoids to range through the menu twice. This solution will probably cease to work with the next Hugo release. It was fragile because the menu had to be rendered always before the content.

Is there a way to read a menu parameter from the front-matter directly?

Thanks, Georg

Are the menu entries in the side bar from site.Menus.something?

If yes, do the menu entries specify pageRef instead of url?

Or do you specifiy the menu to which the page belongs in front matter?

The menu entries for the sidebar are always configured in the frontmatter of a page. The menu map is site.Menus.doc but there a no url or pageref parameters necessary.

The repo is perplexdoc with this theme.

I haven’t looked at the repo.

Assuming front matter like this:

title = 'Post 1'
date = 2024-02-04T13:38:07-08:00
draft = false
pre = 'foo-post-1'

From the template rendering the content you can do:

{{ }}

That works in v0.122.0 and v0.123.0-DEV

Oh, many thanks, that’s nice and easy. I must have overlooked this possibility.

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.