Hello everyone,
I’m quiet new to Hugo and im the past few days im having a Problem with the navbar that im desinging.
i wanted to make a navbar where one of the points has a dropdown with more navigationlinks. I had this problem alerady when tring to use a “enabledropdown” value for an if statment whether or not to use a dropdown with a specific navpoint. i solved this by simply comparing the name of the navpoint and the preset value.
So my link in named “Products” and right now im just comparing if the name is “Products” and if yes im showing a dropdown.
When imply using the “enableDropdown = true” and a {{ if .enableDropdown }} within a {{ range .Site.Menus }} function gave me this ERROR : can’t evaluate field enableDropdown in type *hugolib.MenuEntry.
Now im also tring to make the points of the dropdown adressable in the config.toml and i get the same ERROR.
config.toml:
[menu]
[[menu.header]]
weight = 2
name = "Solutions"
ur = "/solutions"
[[menu.header]]
weight = 3
name = "Products"
ur = "#"
[[menu.header]]
weight = 4
name = "Refernces"
ul = "/referenzen"
[[menu.header]]
weight = 5
name = "Contact"
ul = "/contact"
[[menu.header]]
weight = 5
name = "Impressum"
ul = "/impressum"
[[menu.header.dropdown]]
name = "Test1"
ul = "#"
[[menu.header.dropdown]]
name = "Test2"
ul = "#"
{{ end }}
ERROR:
Building sites … ERROR 2018/08/01 10:04:33 Error while rendering “page” in “”: template: page/single.html:8:5: executing “page/single.html” at <partial .Params.id .>: error calling partial: template: partials/solutions.html:2:3: executing “partials/solutions.html” at <partial "header_solu…>: error calling partial: template: partials/header_solution.html:31:33: executing “partials/header_solution.html” at <.dropdown>: can’t evaluate field dropdown in type *hugolib.MenuEntry
Total in 180 ms
Error: Error building site: logged 1 error(s)
Any tipps are welcome
I don’t think I have a public repo with menus configured. How have you set up your menu in your config.toml? And how are you generating the menu in your layout?
I cant upload the whole site right now, need to talk to my boss first. But here are the important parts: https://github.com/BashCS/dropdown
I hope this helps
Below is my site-navigation.html. I have tried to include your code from header.html that you shared. The “About” section on main nav is where I am hoping to place a submenu. I kept some of your code, hoping to get the menu to partially work.
I have spent many hours working on this and I am completely confused at this point. Apologies.
Your menu config does not have children defined, so .HasChildren always returns false. Please refer to the docs here: https://gohugo.io/content-management/menus/#nesting or the second post above for examples of how to nest menus.
I’m a little confused as to what you are trying to do in the {{if .HasChildren}} block. What you have here is that it will print out all the pages in the About and Product sections to any menu where .HasChildren is true.
This is what I am assuming you wish to accomplish:
range through all menus configured
if the menu item is About or Product(?) and has children, put the children in a dropdown?
else treat as regular menu
If my assumptions above are accurate, you have at least two options:
Configure menu entry for each page under About (or whatever applicable) section either in its own page frontmatter or on the config.toml (see above links to docs for how to accomplish), then something like
<!-- code untested -->
{{ range .Site.Menus.main }}
{{ if .HasChildren }}
{{.Name}}<!--this is the parent-->
<div class="dropdown">
{{ range .Children }}<!-- these are the children menu items -->
<a href="{{.URL}}">{{.Name}}</a>
{{ end }}
</div>
{{ else }}
{{ .Name }}<!-- No children menu -->
{{ end }}
{{ end }}
If you don’t want to config the individual pages’ menu entries, and you know which sections specifically you want to display the children of, something like this probably works:
<!-- code untested -->
{{ range .Site.Menus.main }}
{{ if eq .Identifier "about" }}<!-- you have to do this for each menu item that has children you want to display -->
{{ range where $.Pages "Section" .Identifier }}
<!-- assuming .Identifier matches section, otherwise replace with "about" -->
<a href="{{.RelPermalink}}">{{.Params.title}}</a>
{{ end }}
{{ else }}
<a href="{{.URL}}">{{.Name}}</a>
{{ end }}
{{ end }}