ByWeight
Syntax
MENU.ByWeight
Returns
navigation.Menu
ByWeight
方法返回按权重
,名称,标识符排序的给定菜单。这是默认的排序顺序。
考虑以下的菜单定义:
hugo.
menu:
main:
- identifier: about
name: 关于
pageRef: /about
weight: 20
- identifier: services
name: 服务
pageRef: /services
weight: 10
- identifier: contact
name: 联系
pageRef: /contact
weight: 30
[menu]
[[menu.main]]
identifier = 'about'
name = '关于'
pageRef = '/about'
weight = 20
[[menu.main]]
identifier = 'services'
name = '服务'
pageRef = '/services'
weight = 10
[[menu.main]]
identifier = 'contact'
name = '联系'
pageRef = '/contact'
weight = 30
{
"menu": {
"main": [
{
"identifier": "about",
"name": "关于",
"pageRef": "/about",
"weight": 20
},
{
"identifier": "services",
"name": "服务",
"pageRef": "/services",
"weight": 10
},
{
"identifier": "contact",
"name": "联系",
"pageRef": "/contact",
"weight": 30
}
]
}
}
要按权重
,名称,标识符排序菜单项:
<ul>
{{ range .Site.Menus.main.ByWeight }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
</ul>
Hugo 渲染为:
<ul>
<li><a href="/services/">服务</a></li>
<li><a href="/about/">关于</a></li>
<li><a href="/contact">联系</a></li>
</ul>
还可以使用sort
函数对菜单项进行排序。例如,按权重
以降序排序:
<ul>
{{ range sort .Site.Menus.main "Weight" "desc" }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
</ul>
当在菜单项使用 sort 函数时,可以使用以下任意一个键进行排序:Identifier
, Name
, Parent
, Post
, Pre
, Title
, URL
, 或 Weight
。