Reverse(反转)
Syntax
MENU.Reverse
Returns
navigation.Menu
Reverse
方法返回给定菜单,反转其条目的排序顺序。
考虑以下菜单定义:
hugo.
menu:
main:
- name: Services
pageRef: /services
weight: 10
- name: About
pageRef: /about
weight: 20
- name: Contact
pageRef: /contact
weight: 30
[menu]
[[menu.main]]
name = 'Services'
pageRef = '/services'
weight = 10
[[menu.main]]
name = 'About'
pageRef = '/about'
weight = 20
[[menu.main]]
name = 'Contact'
pageRef = '/contact'
weight = 30
{
"menu": {
"main": [
{
"name": "Services",
"pageRef": "/services",
"weight": 10
},
{
"name": "About",
"pageRef": "/about",
"weight": 20
},
{
"name": "Contact",
"pageRef": "/contact",
"weight": 30
}
]
}
}
要按名称降序排序条目:
<ul>
{{ range .Site.Menus.main.ByName.Reverse }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
</ul>
Hugo 渲染结果为:
<ul>
<li><a href="/services/">服务</a></li>
<li><a href="/contact">联系人</a></li>
<li><a href="/about/">关于</a></li>
</ul>