hugo/docs/content/en/variables/menus.md

3.6 KiB

title linktitle description date publishdate lastmod categories keywords draft menu weight sections_weight aliases toc
Menu Entry Properties Menu Entry Properties A menu entry in a menu-template has specific variables and functions to make menu management easier. 2017-03-12 2017-03-12 2017-03-12
variables and params
menus
false
docs
title parent weight
variables defined by a menu entry variables 50
50 50
/variables/menu/
false

A menu entry has the following properties available that can be used in a menu template.

Menu Entry Variables

.Menu
string
Name of the menu that contains this menu entry.
.URL
string
URL that the menu entry points to. The url key, if set for the menu entry, sets this value. If that key is not set, and if the menu entry is set in a page front-matter, this value defaults to the page's .RelPermalink.
.Page
*Page
Reference to the page object associated with the menu entry. This will be non-nil if the menu entry is set via a page's front-matter and not via the site config.
.Name
string
Name of the menu entry. The name key, if set for the menu entry, sets this value. If that key is not set, and if the menu entry is set in a page front-matter, this value defaults to the page's .LinkTitle.
.Identifier
string
Value of the identifier key if set for the menu entry. This value must be unique for each menu entry. It is necessary to set a unique identifier manually if two or more menu entries have the same .Name.
.Pre
template.HTML
Value of the pre key if set for the menu entry. This value typically contains a string representing HTML.
.Post
template.HTML
Value of the post key if set for the menu entry. This value typically contains a string representing HTML.
.Weight
int
Value of the weight key if set for the menu entry. If that key is not set, and if the menu entry is set in a page front-matter, this value defaults to the page's .Weight.
.Parent
string
Name (or Identifier if present) of this menu entry's parent menu entry. The parent key, if set for the menu entry, sets this value. If this key is set, this menu entry nests under that parent entry, else it nests directly under the .Menu.
.Children
Menu
This value is auto-populated by Hugo. It is a collection of children menu entries, if any, under the current menu entry.

Menu Entry Functions

Menus also have the following functions available:

.HasChildren
boolean
Returns true if .Children is non-nil.
.KeyName
string
Returns the .Identifier if present, else returns the .Name.
.IsEqual
boolean
Returns true if the two compared menu entries represent the same menu entry.
.IsSameResource
boolean
Returns true if the two compared menu entries have the same .URL.
.Title
string
Link title, meant to be used in the title attribute of a menu entry's <a>-tags. Returns the menu entry's title key if set. Else, if the menu entry was created through a page's front-matter, it returns the page's .LinkTitle. Else, it just returns an empty string.

Additionally, here are some relevant methods available to menus on a page:

.IsMenuCurrent
(menu string, menuEntry *MenuEntry ) boolean
See .IsMenuCurrent method.
.HasMenuCurrent
(menu string, menuEntry *MenuEntry) boolean
See .HasMenuCurrent method.