Page
無論您如何定義選單項目,只要某項目與頁面相關聯,該項目便可以存取該頁面的方法。
在以下的選單定義中,前兩個項目與頁面相關聯,而最後一個項目則無關聯:
hugo.
menus:
main:
- pageRef: /about
weight: 10
- pageRef: /contact
weight: 20
- name: Hugo
url: https://gohugo.io
weight: 30
[menus]
[[menus.main]]
pageRef = '/about'
weight = 10
[[menus.main]]
pageRef = '/contact'
weight = 20
[[menus.main]]
name = 'Hugo'
url = 'https://gohugo.io'
weight = 30
{
"menus": {
"main": [
{
"pageRef": "/about",
"weight": 10
},
{
"pageRef": "/contact",
"weight": 20
},
{
"name": "Hugo",
"url": "https://gohugo.io",
"weight": 30
}
]
}
}
在這個範例中,如果選單項目與頁面相關聯,我們會在錨點元素中使用該頁面的 RelPermalink
和 LinkTitle
。
如果選單項目未與頁面相關聯,我們則會使用其 url
和 name
屬性。
<ul>
{{ range .Site.Menus.main }}
{{ with .Page }}
<li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
{{ else }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
{{ end }}
</ul>
更多資訊請參閱選單範本章節。