Merge commit '5be51ac3db225d5df501ed1fa1499c41d97dbf65'

This commit is contained in:
Bjørn Erik Pedersen
2025-04-10 13:04:51 +02:00
987 changed files with 12379 additions and 14083 deletions

View File

@@ -1,15 +1,9 @@
---
title: Menus
title: Menu templates
description: Create templates to render one or more menus.
categories: [templates]
categories: []
keywords: []
menu:
docs:
identifier: templates-menu
parent: templates
weight: 180
weight: 180
toc: true
weight: 150
aliases: [/templates/menus/,/templates/menu-templates/]
---
@@ -29,7 +23,7 @@ The example below handles every combination.
This partial template recursively "walks" a menu structure, rendering a localized, accessible nested list.
{{< code file=layouts/partials/menu.html copy=true >}}
```go-html-template {file="layouts/partials/menu.html" copy=true}
{{- $page := .page }}
{{- $menuID := .menuID }}
@@ -72,14 +66,14 @@ This partial template recursively "walks" a menu structure, rendering a localize
</li>
{{- end }}
{{- end }}
{{< /code >}}
```
Call the partial above, passing a menu ID and the current page in context.
{{< code file=layouts/_default/single.html >}}
```go-html-template {file="layouts/_default/single.html"}
{{ partial "menu.html" (dict "menuID" "main" "page" .) }}
{{ partial "menu.html" (dict "menuID" "footer" "page" .) }}
{{< /code >}}
```
## Page references
@@ -87,7 +81,7 @@ Regardless of how you [define menu entries], an entry associated with a page has
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.
{{< code file=layouts/_default/single.html >}}
```go-html-template {file="layouts/_default/single.html"}
{{- range site.Menus.main }}
<a href="{{ .URL }}">
{{ .Name }}
@@ -98,7 +92,7 @@ This simplistic example renders a page parameter named `version` next to each en
{{- end }}
</a>
{{- end }}
{{< /code >}}
```
## Menu entry parameters
@@ -109,13 +103,13 @@ When you define menu entries [in site configuration] or [in front matter], you c
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.
{{< code file=layouts/partials/menu.html >}}
```go-html-template {file="layouts/partials/menu.html"}
{{- range site.Menus.main }}
<a {{ with .Params.class -}} class="{{ . }}" {{ end -}} href="{{ .URL }}">
{{ .Name }}
</a>
{{- end }}
{{< /code >}}
```
## Localize
@@ -127,7 +121,7 @@ Hugo provides two methods to localize your menu entries. See [multilingual].
[in front matter]: /content-management/menus/#define-in-front-matter
[in site configuration]: /content-management/menus/#define-in-site-configuration
[localize the menu entries]: /content-management/multilingual/#menus
[menu entry defined in front matter]: /content-management/menus/#example-front-matter
[menu entry defined in site configuration]: /content-management/menus/#example-site-configuration
[menu entry defined in front matter]: /content-management/menus/#example
[menu entry defined in site configuration]: /configuration/menus
[menu methods]: /methods/menu/
[multilingual]: /content-management/multilingual/#menus