mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-21 20:46:30 -05:00
parent
e4fcb672ed
commit
8f5c9a747f
4 changed files with 91 additions and 0 deletions
|
@ -113,6 +113,24 @@ This means that `.Title` will be used unless `.LinkTitle` is present, etc. In pr
|
||||||
|
|
||||||
In this example, the top level of the menu is defined in your [site `config` file][config]. All content entries are attached to one of these entries via the `.Parent` field.
|
In this example, the top level of the menu is defined in your [site `config` file][config]. All content entries are attached to one of these entries via the `.Parent` field.
|
||||||
|
|
||||||
|
## Params
|
||||||
|
|
||||||
|
You can also add user-defined content to menu items via the `params` field.
|
||||||
|
|
||||||
|
A common use case is to define a custom param to add a css class to a specific menu item.
|
||||||
|
|
||||||
|
{{< code-toggle file="config" >}}
|
||||||
|
[[menu.main]]
|
||||||
|
name = "about hugo"
|
||||||
|
pre = "<i class='fa fa-heart'></i>"
|
||||||
|
weight = -110
|
||||||
|
identifier = "about"
|
||||||
|
url = "/about/"
|
||||||
|
[menu.main.params]
|
||||||
|
class = "highlight-menu-item"
|
||||||
|
{{</ code-toggle >}}
|
||||||
|
|
||||||
|
|
||||||
## Render Menus
|
## Render Menus
|
||||||
|
|
||||||
See [Menu Templates](/templates/menu-templates/) for information on how to render your site menus within your templates.
|
See [Menu Templates](/templates/menu-templates/) for information on how to render your site menus within your templates.
|
||||||
|
|
|
@ -160,3 +160,23 @@ Here's an example:
|
||||||
{{ end }}
|
{{ end }}
|
||||||
</nav>
|
</nav>
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Using .Params in Menus
|
||||||
|
|
||||||
|
User-defined content on menu items are accessible via `.Params`.
|
||||||
|
|
||||||
|
Here's an example:
|
||||||
|
|
||||||
|
```
|
||||||
|
<nav class="sidebar-nav">
|
||||||
|
{{ range .Site.Menus.main }}
|
||||||
|
<a href="{{ .URL }}" title="{{ .Title }}" class="{{ with .Params.class }}{{ . }}{{ end }}">
|
||||||
|
{{- .Name -}}
|
||||||
|
</a>
|
||||||
|
{{ end }}
|
||||||
|
</nav>
|
||||||
|
```
|
||||||
|
|
||||||
|
{{% note %}}
|
||||||
|
With Menu-level .Params they can easily exist on one menu item but not another. It's recommended to access them gracefully using the [with function](/functions/with).
|
||||||
|
{{% /note %}}
|
|
@ -267,3 +267,53 @@ menu:
|
||||||
|
|
||||||
b.AssertFileContent("public/index.html", "A|Children:C|B|")
|
b.AssertFileContent("public/index.html", "A|Children:C|B|")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestMenuParams(t *testing.T) {
|
||||||
|
|
||||||
|
b := newTestSitesBuilder(t).WithSimpleConfigFile()
|
||||||
|
|
||||||
|
b.WithTemplatesAdded("index.html", `
|
||||||
|
Main: {{ len .Site.Menus.main }}
|
||||||
|
{{ range .Site.Menus.main }}
|
||||||
|
* Main|{{ .Name }}: {{ .URL }}|{{ .Params }}
|
||||||
|
{{ end }}
|
||||||
|
`)
|
||||||
|
|
||||||
|
b.WithContent("blog/page1.md", `
|
||||||
|
---
|
||||||
|
title: "P1"
|
||||||
|
menu: main
|
||||||
|
---
|
||||||
|
|
||||||
|
`)
|
||||||
|
|
||||||
|
b.WithContent("blog/page2.md", `
|
||||||
|
---
|
||||||
|
title: "P2"
|
||||||
|
menu: main
|
||||||
|
---
|
||||||
|
|
||||||
|
`)
|
||||||
|
|
||||||
|
b.WithContent("blog/page3.md", `
|
||||||
|
---
|
||||||
|
title: "P3"
|
||||||
|
menu:
|
||||||
|
main:
|
||||||
|
weight: 30
|
||||||
|
params:
|
||||||
|
foo: "bar"
|
||||||
|
key2: "value2"
|
||||||
|
---
|
||||||
|
`)
|
||||||
|
|
||||||
|
b.Build(BuildCfg{})
|
||||||
|
|
||||||
|
b.AssertFileContent("public/index.html",
|
||||||
|
"Main: 3",
|
||||||
|
"Main|P3: /blog/page3/|map[foo:bar key2:value2]",
|
||||||
|
"Main|P1: /blog/page1/|map[]",
|
||||||
|
"Main|P2: /blog/page2/|map[]",
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -39,6 +39,7 @@ type MenuEntry struct {
|
||||||
Weight int
|
Weight int
|
||||||
Parent string
|
Parent string
|
||||||
Children Menu
|
Children Menu
|
||||||
|
Params maps.Params
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MenuEntry) URL() string {
|
func (m *MenuEntry) URL() string {
|
||||||
|
@ -127,6 +128,8 @@ func (m *MenuEntry) MarshallMap(ime map[string]interface{}) {
|
||||||
m.Identifier = cast.ToString(v)
|
m.Identifier = cast.ToString(v)
|
||||||
case "parent":
|
case "parent":
|
||||||
m.Parent = cast.ToString(v)
|
m.Parent = cast.ToString(v)
|
||||||
|
case "params":
|
||||||
|
m.Params = maps.ToStringMap(v)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue