限制
Syntax
MENU.Limit N
Returns
navigation.Menu
Limit
方法返回给定菜单,限制为前N个条目。
考虑以下菜单定义:
hugo.
menu:
main:
- name: 服务
pageRef: /services
weight: 10
- name: 关于
pageRef: /about
weight: 20
- name: 联系我们
pageRef: /contact
weight: 30
[menu]
[[menu.main]]
name = '服务'
pageRef = '/services'
weight = 10
[[menu.main]]
name = '关于'
pageRef = '/about'
weight = 20
[[menu.main]]
name = '联系我们'
pageRef = '/contact'
weight = 30
{
"menu": {
"main": [
{
"name": "服务",
"pageRef": "/services",
"weight": 10
},
{
"name": "关于",
"pageRef": "/about",
"weight": 20
},
{
"name": "联系我们",
"pageRef": "/contact",
"weight": 30
}
]
}
}
要按名称进行排序,并限制为前2个条目:
<ul>
{{ range .Site.Menus.main.ByName.Limit 2 }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
</ul>
Hugo渲染为:
<ul>
<li><a href="/about/">关于</a></li>
<li><a href="/contact">联系我们</a></li>
</ul>