| 1 |
--- |
| 2 |
title: Menus |
| 3 |
description: Returns a collection of menu objects for the given site. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: navigation.Menus |
| 9 |
signatures: [SITE.Menus] |
| 10 |
--- |
| 11 |
|
| 12 |
The `Menus` method on a `Site` object returns a collection of menus, where each menu contains one or more entries, either flat or nested. Each entry points to a page within the site, or to an external resource. |
| 13 |
|
| 14 |
> [!note] |
| 15 |
> Menus can be defined and localized in several ways. Please see the [menus] section for a complete explanation and examples. |
| 16 |
|
| 17 |
A site can have multiple menus. For example, a main menu and a footer menu: |
| 18 |
|
| 19 |
{{< code-toggle file=hugo >}} |
| 20 |
[[menus.main]] |
| 21 |
name = 'Home' |
| 22 |
pageRef = '/' |
| 23 |
weight = 10 |
| 24 |
|
| 25 |
[[menus.main]] |
| 26 |
name = 'Books' |
| 27 |
pageRef = '/books' |
| 28 |
weight = 20 |
| 29 |
|
| 30 |
[[menus.main]] |
| 31 |
name = 'Films' |
| 32 |
pageRef = '/films' |
| 33 |
weight = 30 |
| 34 |
|
| 35 |
[[menus.footer]] |
| 36 |
name = 'Legal' |
| 37 |
pageRef = '/legal' |
| 38 |
weight = 10 |
| 39 |
|
| 40 |
[[menus.footer]] |
| 41 |
name = 'Privacy' |
| 42 |
pageRef = '/privacy' |
| 43 |
weight = 20 |
| 44 |
{{< /code-toggle >}} |
| 45 |
|
| 46 |
This template renders the main menu: |
| 47 |
|
| 48 |
```go-html-template |
| 49 |
{{ with site.Menus.main }} |
| 50 |
<nav class="menu"> |
| 51 |
{{ range . }} |
| 52 |
{{ if $.IsMenuCurrent .Menu . }} |
| 53 |
<a class="active" aria-current="page" href="{{ .URL }}">{{ .Name }}</a> |
| 54 |
{{ else }} |
| 55 |
<a href="{{ .URL }}">{{ .Name }}</a> |
| 56 |
{{ end }} |
| 57 |
{{ end }} |
| 58 |
</nav> |
| 59 |
{{ end }} |
| 60 |
``` |
| 61 |
|
| 62 |
When viewing the home page, the result is: |
| 63 |
|
| 64 |
```html |
| 65 |
<nav class="menu"> |
| 66 |
<a class="active" aria-current="page" href="/">Home</a> |
| 67 |
<a href="/books/">Books</a> |
| 68 |
<a href="/films/">Films</a> |
| 69 |
</nav> |
| 70 |
``` |
| 71 |
|
| 72 |
When viewing the `books` page, the result is: |
| 73 |
|
| 74 |
```html |
| 75 |
<nav class="menu"> |
| 76 |
<a href="/">Home</a> |
| 77 |
<a class="active" aria-current="page" href="/books/">Books</a> |
| 78 |
<a href="/films/">Films</a> |
| 79 |
</nav> |
| 80 |
``` |
| 81 |
|
| 82 |
You will typically render a menu using a _partial_ template. As the active menu entry will be different on each page, use the [`partial`] function to call the template. Do not use the [`partialCached`] function. |
| 83 |
|
| 84 |
The example above is simplistic. Please see the [menu templates] section for more information. |
| 85 |
|
| 86 |
[`partial`]: /functions/partials/include/ |
| 87 |
[`partialCached`]: /functions/partials/includecached/ |
| 88 |
[menu templates]: /templates/menu/ |
| 89 |
[menus]: /content-management/menus/ |