Menu
Hugo 的 Menu
- 在Site的變數中有個Menus變數 ⇒
.Site.Menus
Menu
的實體屬性 VARIABLES AND PARAMS Menu Entry Properties- 要將文章加入Menus需要透過front matter
直接將文章加入Menu
menu: "main"
將文章加入多個Menu
menu: ["main", "footer"]
將文章加入某個選單裡
menu:
docs:
parent: 'extras'
weight: 20
title: Menu Templates
linktitle: Menu Templates
menu:
docs:
title: "how to use menus in templates"
parent: "templates"
weight: 130
加入非文章內容到 menu
- 加入非文章內容需修改config.toml,而不是文章的front matter
[menu]
[[menu.main]]
identifier = "about"
name = "about hugo"
pre = "<i class='fa fa-heart'></i>"
url = "/about/"
weight = -110
[[menu.main]]
name = "getting started"
post = "<span class='alert'>New!</span>"
pre = "<i class='fa fa-road'></i>"
url = "/getting-started/"
weight = -100
Menu相關的的函式
Page.IsMenuCurrent MENU MENUENTRY
- IsMenuCurrent是Page物件裡的一個函式,會回傳布林值
- 判斷目前的Page和MenuEntry裡的Menu是否為同一個物件 ⇒ 是否同一個頁面
- 是則回傳true
EX: 判斷$currentPage是否和.裡的"main"為同一個頁面
<li class="{{ if $currentPage.IsMenuCurrent "main" . }}active{{ end }}">
PAGE.HasMenuCurrent MENU MENUENTRY
- HasMenuCurrent 是Page物件裡的一個函式,會回傳布林值
- 判斷目前的Page是否為MenuEntry的Menu的子選單之一
EX: 判斷$currentPage是否為.裡的main選單的子選單
<li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
It returns true if the PAGE is the same object as the .Page in one of the children menu entries under MENUENTRY in a given MENU.
Menu實體變數(Menu Entry Variables)
.Menu
擁有這個menu實體的menu的名字字串
.URL
- menu實體指向的URL
- 如果沒有設定URL,預設為 .RelPermalink
.Name
- menu實體的名字字串
- 預設為Page的 .LinkTitle
.Identifier
- 對每個menu實體都是唯一的Key
- 如果有2個menu實體都有相同的.Name,則一定要設.Identifier
.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
.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 -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.
官網範例
Slidebar
<!-- sidebar start -->
<aside>
<ul>
{{ $currentPage := . }}
{{ range .Site.Menus.main }}
{{ if .HasChildren }}
<li class="{{ if $currentPage.HasMenuCurrent "main" . }}active{{ end }}">
<a href="#">
{{ .Pre }}
<span>{{ .Name }}</span>
</a>
</li>
<ul class="sub-menu">
{{ range .Children }}
<li class="{{ if $currentPage.IsMenuCurrent "main" . }}active{{ end }}">
<a href="{{ .URL }}">{{ .Name }}</a>
</li>
{{ end }}
</ul>
{{ else }}
<li>
<a href="{{ .URL }}">
{{ .Pre }}
<span>{{ .Name }}</span>
</a>
</li>
{{ end }}
{{ end }}
<li>
<a href="#" target="_blank">Hardcoded Link 1</a>
</li>
<li>
<a href="#" target="_blank">Hardcoded Link 2</a>
</li>
</ul>
</aside>