fjk
May 7, 2023, 3:46pm
1
I am trying to add custom variables to menu entries. My site is multilingual and I added the navigation items to a translated menu inside config/_default/languages.toml
Example:
[en]
weight = 1
languageName = 'English'
contentDir = "content/en"
[[en.menu.main]]
name = 'About us'
pageRef = '/about'
weight = 10
action = 'ContactAction'
[[en.menu.main]]
name = 'Career'
pageRef = '/career'
weight = 20
action = 'ReadJobPostingAction'
I was reading through this support question to find out how to attach custom action
attributes to a menu
.
fjk
May 7, 2023, 3:47pm
2
While stated otherwise, it turns out, this actually is supported :
{{- range $a := .Site.Params.menus.main }}
{{ printf "%#v" $a.action }}
{{- end }}
which prints
"ContactAction"
"ReadJobPostingAction"
The topic you refer to is wildly inaccurate. These statements are utterly false:
Currently there is no support for custom menu variables.
So the bottom line is: no custom properties in menu definitions.
The params
menu entry property is described here:
https://gohugo.io/content-management/menus/#properties-front-matter
Examples of menu entries with a params
table:
https://gohugo.io/content-management/menus/#example-front-matter
https://gohugo.io/content-management/menus/#example-site-configuration
The .Params
menu entry variable is described here:
https://gohugo.io/variables/menus/#variables
An example of rendering a menu entry parameter:
https://gohugo.io/templates/menu-templates/#menu-entry-parameters
I have no idea why you placed menu items under the site configuration’s params
table, but this is the wrong way to do it.
fjk
May 7, 2023, 4:39pm
4
Thanks for making things clear.
I have no idea why you placed menu items under the site configuration’s params
table, but this is the wrong way to do it.
Well, I did not. They only reside inside config/default/languages.toml
as shown above. Anyway, thanks to your links, I changed it to the following, which works:
[en]
weight = 1
languageName = 'English'
contentDir = "content/en"
[[en.menu.main]]
name = 'About us'
pageRef = '/about'
weight = 10
[en.menu.main.params]
action = 'ContactAction'
[[en.menu.main]]
name = 'Career'
pageRef = '/career'
weight = 20
[en.menu.main.params]
action = 'ReadJobPostingAction'
Now the following is what I can use:
{{- range $i, $e := site.Menus.main }}{{ if $i }}, {{- end }}
{{ with .Params -}}{{ .action }}{{ end -}}
{{ $e.Name }}
{{ $e.PageRef | absURL }}
{{- end }}
system
Closed
May 9, 2023, 4:39pm
5
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.