Custom variables in menus

Relatively new to Hugo… I must be overlooking something trivial.

In my config I have menus

[menu]
  [[menu.main]]
    identifier = "home"
    target = "left"
    name = "Over ons"
    title = "Over ons"
    url  = "home/"
    weight = 1

etc.

From the menu handling partials, I can access the properties with predefined functions, e.g. .URL for the url, .Name for the name and so on.

But how can I get the value of the target property?

Hello,

Currently there is no support for custom menu variables.

How do you intend to use the target parameter?

There may be another way to output the value of target based on a condition like for example if .Name has a certain value etc.

1 Like

I use the menu definitions to implement icon menus. On wide screen, there are four icons vertically stacked to the left, and four to the right. On small screen there is one horizontal set of all eight icons. I have three partials for that (iconsleft, iconsright and iconstop).

So I wanted in e.g. iconsleft something like:

{{ range .Site.Menus.main }}
{{ if eq .Target "left" }}
<div class="icon"> ... </div>
{{ end }}
{{ end }}

You get the idea.

Right. I suppose that you could prefix the menu entry identifiers with values for the respective icon menus. e.g. identifier = "left_home"

So that the above would become
{{ if in .Identifier "left" }}

Interesting suggestion. Unfortunately, the identifier is also used for the icon image, folders etc.
Maybe I can abuse .Pre or .Post since I (currently) do not use these.

Surprising is that it is possible and legal to add custom properties in the menu definitions but there’s just not a function to inspect them.

I assume that you need a way to debug.

We typically use the printf function for template debugging. See:

No, as explained in posting #3 I would like to use their values.

Anyway, I’ve left this approach for now since there only one occasion that I’d need it for, and it is easier to copy and adapt the code fragments.

Ok. Glad you found another approach but I’m afraid that I don’t follow.

Within the menu context as posted in in post #3 you should be able to get the values. If that is not possible then something else is going on and we need to see the full project or you would need to share a sample project that reproduces what you encountered.

Now it is my turn to not follow…

Do you mean that with the setup as posted in #1 and #3 it should be possible to use the value of the target property inside the partial?

In #2 you replied “Currently there is no support for custom menu variables.” so I understood this approach was not going to work. If I try I get the error can't evaluate field target :

Failed to render pages: render of "page" failed: execute of template failed: template: _default/single.html:8:10: executing "_default/single.html" at <partial "iconsleft.html" .>: error calling partial: "/bulk/tmp/quickstart/themes/september/layouts/partials/iconsleft.html:2:9": execute of template failed: template: partials/iconsleft.html:2:9: executing "partials/iconsleft.html" at <.target>: can't evaluate field target in type *navigation.MenuEntry

I now see that I misread the above post.

I have just opened the following GitHub issue:

Hugo should throw a build breaking ERROR when a custom entry property is encountered in the config.

So the bottom line is: no custom properties in menu definitions. That’s clear.
Thanks.

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