Add menu params
authorDavid Jones <david@davidejones.com>
Sun, 22 Nov 2020 21:09:59 +0000 (13:09 -0800)
committerGitHub <noreply@github.com>
Sun, 22 Nov 2020 21:09:59 +0000 (22:09 +0100)
Fixes #7951

docs/content/en/content-management/menus.md
docs/content/en/templates/menu-templates.md
hugolib/menu_test.go
navigation/menu.go

index 9ac6f8bff533ceff15fc18fde2fe917327d78da0..6b89c44dab3a418578e29bccf799fa4130b1b90f 100644 (file)
@@ -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.
 
+## 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
 
 See [Menu Templates](/templates/menu-templates/) for information on how to render your site menus within your templates.
index b39fe42a927bceb03a7f069ca8c1a876e6e0709f..8893d7b5a0badaa71bd873515a3dbaa087fd243b 100644 (file)
@@ -160,3 +160,23 @@ Here's an example:
   {{ end }}
 </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 %}}
\ No newline at end of file
index 6fa31b4ee7a5106cd5d0014f19909c77905c79f6..cfb4b954be21022f7de8a389e1974c267b5cea04 100644 (file)
@@ -267,3 +267,53 @@ menu:
 
        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[]",
+       )
+
+}
index ae2e0e4ff9fc77accc9bd6d753a798de6321e764..498078e34a75b76e571a800dcdbad3934d07ad7d 100644 (file)
@@ -39,6 +39,7 @@ type MenuEntry struct {
        Weight        int
        Parent        string
        Children      Menu
+       Params        maps.Params
 }
 
 func (m *MenuEntry) URL() string {
@@ -127,6 +128,8 @@ func (m *MenuEntry) MarshallMap(ime map[string]interface{}) {
                        m.Identifier = cast.ToString(v)
                case "parent":
                        m.Parent = cast.ToString(v)
+               case "params":
+                       m.Params = maps.ToStringMap(v)
                }
        }
 }