Debian Codegraph @

docs/content/en/templates/menu.md

raw
1 ---
2 title: Menu templates
3 description: Create templates to render one or more menus.
4 categories: []
5 keywords: []
6 weight: 150
7 aliases: [/templates/menus/,/templates/menu-templates/]
8 ---
9
10 ## Overview
11
12 After [defining menu entries][], use [menu methods][] to render a menu.
13
14 Three factors determine how to render a menu:
15
16 1. The method used to define the menu entries: [automatic][], in [front matter][], or in your [project configuration][]
17 1. The menu structure: flat or nested
18 1. The method used to [localize the menu entries][]: project configuration or translation tables
19
20 The example below handles every combination.
21
22 ## Example
23
24 This _partial_ template recursively "walks" a menu structure, rendering a localized, accessible nested list.
25
26 ```go-html-template {file="layouts/_partials/menu.html" copy=true}
27 {{- $page := .page }}
28 {{- $menuID := .menuID }}
29
30 {{- with index site.Menus $menuID }}
31 <nav>
32 <ul>
33 {{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
34 </ul>
35 </nav>
36 {{- end }}
37
38 {{- define "_partials/inline/menu/walk.html" }}
39 {{- $page := .page }}
40 {{- range .menuEntries }}
41 {{- $attrs := dict "href" .URL }}
42 {{- if $page.IsMenuCurrent .Menu . }}
43 {{- $attrs = merge $attrs (dict "class" "active" "aria-current" "page") }}
44 {{- else if $page.HasMenuCurrent .Menu .}}
45 {{- $attrs = merge $attrs (dict "class" "ancestor" "aria-current" "true") }}
46 {{- end }}
47 {{- $name := .Name }}
48 {{- with .Identifier }}
49 {{- with T . }}
50 {{- $name = . }}
51 {{- end }}
52 {{- end }}
53 <li>
54 <a
55 {{- range $k, $v := $attrs }}
56 {{- with $v }}
57 {{- printf " %s=%q" $k $v | safeHTMLAttr }}
58 {{- end }}
59 {{- end -}}
60 >{{ $name }}</a>
61 {{- with .Children }}
62 <ul>
63 {{- partial "inline/menu/walk.html" (dict "page" $page "menuEntries" .) }}
64 </ul>
65 {{- end }}
66 </li>
67 {{- end }}
68 {{- end }}
69 ```
70
71 Call the partial above, passing a menu ID and the current page in context.
72
73 ```go-html-template {file="layouts/page.html"}
74 {{ partial "menu.html" (dict "menuID" "main" "page" .) }}
75 {{ partial "menu.html" (dict "menuID" "footer" "page" .) }}
76 ```
77
78 ## Page references
79
80 Regardless of how you [define menu entries][], an entry associated with a page has access to page context.
81
82 This simplistic example renders a page parameter named `version` next to each entry's `name`. Code defensively using `with` or `if` to handle entries where (a) the entry points to an external resource, or (b) the `version` parameter is not defined.
83
84 ```go-html-template {file="layouts/page.html"}
85 {{- range site.Menus.main }}
86 <a href="{{ .URL }}">
87 {{ .Name }}
88 {{- with .Page }}
89 {{- with .Params.version -}}
90 ({{ . }})
91 {{- end }}
92 {{- end }}
93 </a>
94 {{- end }}
95 ```
96
97 ## Menu entry parameters
98
99 When you define menu entries in your [project configuration][] or in [front matter][], you can include a `params` key as shown in these examples:
100
101 - [Menu entry defined in your project configuration][]
102 - [Menu entry defined in front matter][]
103
104 This simplistic example renders a `class` attribute for each anchor element. Code defensively using `with` or `if` to handle entries where `params.class` is not defined.
105
106 ```go-html-template {file="layouts/_partials/menu.html"}
107 {{- range site.Menus.main }}
108 <a {{ with .Params.class -}} class="{{ . }}" {{ end -}} href="{{ .URL }}">
109 {{ .Name }}
110 </a>
111 {{- end }}
112 ```
113
114 ## Localize
115
116 Hugo provides two methods to localize your menu entries. See [multilingual][].
117
118 [Menu entry defined in front matter]: /content-management/menus/#example
119 [Menu entry defined in your project configuration]: /configuration/menus/
120 [automatic]: /content-management/menus/#define-automatically
121 [define menu entries]: /content-management/menus/
122 [defining menu entries]: /content-management/menus/
123 [front matter]: /content-management/menus/#define-in-front-matter
124 [localize the menu entries]: /content-management/multilingual/#menus
125 [menu methods]: /methods/menu/
126 [multilingual]: /content-management/multilingual/#menus
127 [project configuration]: /content-management/menus/#define-in-project-configuration