2014-05-29 22:42:05 +00:00
---
2016-01-06 22:45:19 +00:00
lastmod: 2015-08-04
2014-05-29 22:42:05 +00:00
date: 2014-05-14T02:36:37Z
2015-05-11 16:39:40 +00:00
toc: true
2014-05-29 22:42:05 +00:00
menu:
main:
parent: extras
2016-12-12 00:55:36 +00:00
next: /extras/pagination
2014-05-29 22:42:05 +00:00
prev: /extras/livereload
title: Menus
---
2014-05-27 22:32:57 +00:00
Hugo has a simple yet powerful menu system that permits content to be
2015-08-04 18:00:08 +00:00
placed in menus with a good degree of control without a lot of work.
2014-05-27 22:32:57 +00:00
2015-05-10 14:18:14 +00:00
*TIP:* If all you want is a simple menu for your sections, see [Section Menu for "the Lazy Blogger" ]({{< relref "#section-menu-for-the-lazy-blogger" >}} ).
2014-05-27 22:32:57 +00:00
Some of the features of Hugo Menus:
* Place content in one or many menus
* Handle nested menus with unlimited depth
* Create menu entries without being attached to any content
* Distinguish active element (and active branch)
## What is a menu?
2014-10-07 23:52:58 +00:00
A menu is a named array of menu entries accessible on the site under
2014-09-03 04:12:26 +00:00
`.Site.Menus` by name. For example, if I have a menu called `main` , I would
2014-08-31 11:08:36 +00:00
access it via `.Site.Menus.main` .
2014-05-27 22:32:57 +00:00
2016-09-28 15:17:57 +00:00
If you make use of the [multilingual feature ]({{< relref "content/multilingual.md#menus">}} ) you can define menus language independent.
2014-05-27 22:32:57 +00:00
A menu entry has the following properties:
2017-02-21 09:38:12 +00:00
* `URL string`
* `Name string`
* `Menu string`
* `Identifier string`
* `Pre template.HTML`
* `Post template.HTML`
* `Weight int`
* `Parent string`
* `Children Menu`
2014-05-27 22:32:57 +00:00
2014-05-29 21:40:11 +00:00
And the following functions:
2017-02-21 09:38:12 +00:00
* `HasChildren() bool`
2014-05-27 22:32:57 +00:00
2017-02-21 09:38:12 +00:00
Additionally, the `Page` object has two functions, which can be used when rendering menus:
2014-05-27 22:32:57 +00:00
2017-02-21 09:38:12 +00:00
* `IsMenuCurrent (menu string, menuEntry *MenuEntry ) bool`
* `HasMenuCurrent** (menu string, menuEntry *MenuEntry) bool`
2014-05-27 22:32:57 +00:00
2017-02-21 09:38:12 +00:00
See [Menu Functions ](#menu-functions ) for explanations of these functions, and [Rendering Nested Menus ](#rendering-nested-menus ) for an example of their use.
2014-05-27 22:32:57 +00:00
## Adding content to menus
Hugo supports a couple of different methods of adding a piece of content
to the front matter.
### Simple
If all you need to do is add an entry to a menu, the simple form works
well.
**A single menu:**
---
menu: "main"
---
**Multiple menus:**
---
menu: ["main", "footer"]
---
### Advanced
If more control is required, then the advanced approach gives you the
control you want. All of the menu entry properties listed above are
available.
---
menu:
main:
parent: 'extras'
weight: 20
---
## Adding (non-content) entries to a menu
You can also add entries to menus that aren’ t attached to a piece of
2015-01-28 02:17:09 +00:00
content. This takes place in the sitewide [config file ](/overview/configuration/ ).
2014-05-27 22:32:57 +00:00
2015-01-09 18:51:15 +00:00
Here’ s an example `config.toml` :
2014-05-27 22:32:57 +00:00
[[menu.main]]
name = "about hugo"
pre = "< i class = 'fa fa-heart' > < / i > "
weight = -110
identifier = "about"
2014-12-12 19:28:28 +00:00
url = "/about/"
2014-05-27 22:32:57 +00:00
[[menu.main]]
name = "getting started"
pre = "< i class = 'fa fa-road' > < / i > "
weight = -100
2014-12-12 19:28:28 +00:00
url = "/getting-started/"
2014-05-27 22:32:57 +00:00
2015-01-09 18:51:15 +00:00
And the equivalent example `config.yaml` :
2014-08-15 19:02:24 +00:00
---
menu:
main:
- Name: "about hugo"
Pre: "< i class = 'fa fa-heart' > < / i > "
Weight: -110
Identifier: "about"
2015-03-18 06:44:12 +00:00
URL: "/about/"
2014-08-15 19:02:24 +00:00
- Name: "getting started"
Pre: "< i class = 'fa fa-road' > < / i > "
Weight: -100
2015-03-18 06:44:12 +00:00
URL: "/getting-started/"
2015-08-04 18:00:08 +00:00
---
2014-08-15 19:02:24 +00:00
2014-12-12 19:28:28 +00:00
2016-10-24 18:56:00 +00:00
**NOTE:** The URLs must be relative to the context root. If the `baseURL` is `http://example.com/mysite/` , then the URLs in the menu must not include the context root `mysite` . Using an absolute URL will overide the baseURL. If the `URL` is `http://subdomain.example.com/` , the output will be `http://subdomain.example.com` .
2015-08-04 18:00:08 +00:00
2014-05-27 22:32:57 +00:00
## Nesting
All nesting of content is done via the `parent` field.
The parent of an entry should be the identifier of another entry.
Identifier should be unique (within a menu).
2016-10-24 18:56:00 +00:00
The following order is used to determine an Identifier:
2016-05-20 11:32:52 +00:00
2016-10-24 18:56:00 +00:00
> Name > LinkTitle > Title.
2016-05-20 11:32:52 +00:00
This means that the title will be used unless
2014-05-27 22:32:57 +00:00
linktitle is present, etc. In practice Name and Identifier are never
displayed and only used to structure relationships.
In this example, the top level of the menu is defined in the config file
and all content entries are attached to one of these entries via the
`parent` field.
## Rendering menus
Hugo makes no assumptions about how your rendered HTML will be
2014-08-31 11:08:36 +00:00
structured. Instead, it provides all of the functions you will need to be
2015-08-04 18:00:08 +00:00
able to build your menu however you want.
2014-05-27 22:32:57 +00:00
The following is an example:
<!-- sidebar start -->
< aside >
2015-01-09 18:51:15 +00:00
< div id = "sidebar" class = "nav-collapse" >
2014-05-27 22:32:57 +00:00
<!-- sidebar menu start -->
< ul class = "sidebar-menu" >
2016-11-20 22:00:57 +00:00
{{ $currentPage := . }}
2014-05-27 22:32:57 +00:00
{{ range .Site.Menus.main }}
{{ if .HasChildren }}
2016-11-20 22:00:57 +00:00
< li class = "sub-menu{{if $currentPage.HasMenuCurrent " main " . } } active { { end } } " >
2014-05-27 22:32:57 +00:00
< a href = "javascript:;" class = "" >
{{ .Pre }}
< span > {{ .Name }}< / span >
< span class = "menu-arrow arrow_carrot-right" > < / span >
< / a >
< ul class = "sub" >
{{ range .Children }}
2016-11-20 22:00:57 +00:00
< li { { if $ currentPage . IsMenuCurrent " main " . } } class = "active" { { end } } > < a href = "{{.URL}}" > {{ .Name }} < / a > < / li >
2014-05-27 22:32:57 +00:00
{{ end }}
< / ul >
{{else}}
< li >
2016-09-28 15:17:57 +00:00
< a href = "{{.URL}}" >
2014-05-27 22:32:57 +00:00
{{ .Pre }}
< span > {{ .Name }}< / span >
< / a >
{{end}}
< / li >
{{end}}
< li > < a href = "https://github.com/spf13/hugo/issues" target = "blank" > Questions and Issues< / a > < / li >
< li > < a href = "#" target = "blank" > Edit this Page< / a > < / li >
< / ul >
<!-- sidebar menu end -->
< / div >
< / aside >
<!-- sidebar end -->
2015-05-10 14:18:14 +00:00
2016-09-28 15:17:57 +00:00
> **Note**: use the `absLangURL` or `relLangURL` if your theme makes use of the [multilingual feature]({{< relref "content/multilingual.md" >}}). In contrast to `absURL` and `relURL` it adds the correct language prefix to the url. [Read more]({{< relref "templates/functions.md#urls" >}}).
2015-05-10 14:18:14 +00:00
## Section Menu for "the Lazy Blogger"
To enable this menu, add this to your site config, i.e. `config.toml` :
```
SectionPagesMenu = "main"
```
The menu name can be anything, but take a note of what it is.
This will create a menu with all the sections as menu items and all the sections' pages as "shadow-members". The _shadow_ implies that the pages isn't represented by a menu-item themselves, but this enables you to create a top-level menu like this:
```
< nav class = "sidebar-nav" >
2016-11-20 22:00:57 +00:00
{{ $currentPage := . }}
2015-05-10 14:18:14 +00:00
{{ range .Site.Menus.main }}
2016-11-20 22:00:57 +00:00
< a class = "sidebar-nav-item{{if or ($currentPage.IsMenuCurrent " main " . ) ( $ currentPage . HasMenuCurrent " main " . ) } } active { { end } } " href = "{{.URL}}" > {{ .Name }}< / a >
2015-05-10 14:18:14 +00:00
{{ end }}
< / nav >
```
In the above, the menu item is marked as active if on the current section's list page or on a page in that section.
2015-08-04 18:00:08 +00:00
The above is all that's needed. But if you want custom menu items, e.g. changing weight or name, you can define them manually in the site config, i.e. `config.toml` :
2015-05-10 14:18:14 +00:00
```
[[menu.main]]
name = "This is the blog section"
weight = -110
identifier = "blog"
url = "/blog/"
```
**Note** that the `identifier` must match the section name.
2017-02-21 09:38:12 +00:00
## Menu Functions
Suppose you have the menu structure shown below.
```
[menu.main]
│
├───colour
│ │
│ ├───warm
│ │ ├───orange
│ │ ├───red
│ │ └───yellow
│ │
│ └───cool
│ ├───blue
│ ├───green
│ └───purple
│
└───tool
├───hammer
├───shovel
└───saw
```
For each menu item, you can determine:
* If the menu item has any children: `.HasChildren()`
* If the menu item is a parent of the page you are currently rendering: `.Page.HasMenuCurrent()`
* If the menu item **is** the page you are currently rendering: `.Page.IsMenuCurrent()`
For example, if you are currently rendering the page `/colour/warm` , the values of `.HasChildren` , `HasMenuCurrent` , and `IsMenuCurrent` would be as shown below:
```
[menu.main] | | | |
│ | | | |
├───colour | HasMenuCurrent | | HasChildren |
│ ├───< < WARM > > | | IsMenuCurrent | HasChildren |
│ │ ├───orange | | | |
│ │ ├───red | | | |
│ │ └───yellow | | | |
│ └───cool | | | HasChildren |
│ ├───blue | | | |
│ ├───green | | | |
│ └───purple | | | |
└───tool | | | HasChildren |
├───hammer | | | |
├───shovel | | | |
└───saw | | | |
```
## Rendering nested menus
Hugo supports nested menus with as many levels as you like.
Nested menus can be rendered using a recursive partial template, such as the example below.
```
<!-- layouts/index.html, layouts/_default/single.html, ... -->
< h1 > {{ .Title }}< / h1 >
<!-- Put this line in your main template, at the place where you want to
render the menu. -->
{{ partial "menu_include.html" . }}
```
```
<!-- layouts/partials/menu_include.html -->
{{ partial "menu_recursive.html" (dict "menu" .Site.Menus.main "page" . "site" .Site) }}
```
```
<!-- layouts/partials/menu_recursive.html -->
{{ $page := .page }}
{{ $site := .site }}
< ul >
{{ range .menu }}
{{ $is := $page.IsMenuCurrent "main" . }}
{{ $has := $page.HasMenuCurrent "main" . }}
{{ if .HasChildren }}
< li >
< a href = "{{ .URL }}" >
{{ .Name }}
{{ if $is }}[Is]{{ end }}
{{ if $has }}[Has]{{ end }}
{{ if .HasChildren }}[Children]{{ end }}
< / a >
<!-- If the menu item has children, include this partial template again (recursively) -->
{{ partial "menu_recursive.html" (dict "menu" .Children "page" $page "site" $site) }}
< / li >
{{ else }}
< li >
< a href = "{{ .URL }}" >
{{ .Name }}
{{ if $is }}[Is]{{ end }}
{{ if $has }}[Has]{{ end }}
{{ if .HasChildren }}[Children]{{ end }}
< / a >
< / li >
{{ end }}
{{ end }}
< / ul >
```
This example code renders the words `[Is]` , `[Has]` , and `[Children]` to demonstrate how the `IsMenuCurrent()` , `HasMenuCurrent()` , and `HasChildren()` functions work.
You can customise this example to implement features such as:
* Highlight the current item, by applying a CSS style:
< a href = "{{ .URL }}" { { if $ is } } class = "active" { { end } } >
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* Highlight parents of the current item, by applying a CSS style:
< a href = "{{ .URL }}" { { if $ has } } class = "parent-active" { { end } } >
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
* Only render sub-menus for parents of the current menu item, and the current menu item itself:
{{ if or $is $has }}
{{ partial "menu_recursive.html" (dict "menu" .Children "page" $page "site" $site) }}
{{ end }}
A working example, implementing these features, is shown below.
```
<!-- layouts/partials/menu_recursive.html -->
{{ $page := .page }}
{{ $site := .site }}
< ul >
<!-- Menu items sorted alphabetically by name -->
{{ range .menu.ByName }}
{{ $is := $page.IsMenuCurrent "main" . }}
{{ $has := $page.HasMenuCurrent "main" . }}
{{ if .HasChildren }}
< li >
< a href = "{{ .URL }}" class = "{{ if $is }} active{{ end }}{{ if $has }} parent-active{{ end }}" >
{{ .Name }}
<!-- Show a » symbol if there is a sub - menu we haven't rendered -->
{{ if not (or $is $has) }}»{{ end }}
< / a >
<!-- Only render sub - menu for parent items and the current item -->
{{ if or $is $has }}
{{ partial "menu_recursive.html" (dict "menu" .Children "page" $page "site" $site) }}
{{ end }}
< / li >
{{ else }}
< li >
< a href = "{{ .URL }}" class = "{{ if $is }}active{{end}}" > {{ .Name }}< / a >
< / li >
{{ end }}
{{ end }}
< / ul >
```