子菜单
Syntax
MENUENTRY.Children
Returns
navigation.Menu
在渲染嵌套菜单时,请使用 Children
方法。
使用以下站点配置:
hugo.
menu:
main:
- name: 产品
pageRef: /product
weight: 10
- name: 产品 1
pageRef: /products/product-1
parent: 产品
weight: 1
- name: 产品 2
pageRef: /products/product-2
parent: 产品
weight: 2
[menu]
[[menu.main]]
name = '产品'
pageRef = '/product'
weight = 10
[[menu.main]]
name = '产品 1'
pageRef = '/products/product-1'
parent = '产品'
weight = 1
[[menu.main]]
name = '产品 2'
pageRef = '/products/product-2'
parent = '产品'
weight = 2
{
"menu": {
"main": [
{
"name": "产品",
"pageRef": "/product",
"weight": 10
},
{
"name": "产品 1",
"pageRef": "/products/product-1",
"parent": "产品",
"weight": 1
},
{
"name": "产品 2",
"pageRef": "/products/product-2",
"parent": "产品",
"weight": 2
}
]
}
}
并使用以下模板:
<ul>
{{ range .Site.Menus.main }}
<li>
<a href="{{ .URL }}">{{ .Name }}</a>
{{ if .HasChildren }}
<ul>
{{ range .Children }}
<li><a href="{{ .URL }}">{{ .Name }}</a></li>
{{ end }}
</ul>
{{ end }}
</li>
{{ end }}
</ul>
Hugo 会渲染出以下 HTML:
<ul>
<li>
<a href="/products/">产品</a>
<ul>
<li><a href="/products/product-1/">产品 1</a></li>
<li><a href="/products/product-2/">产品 2</a></li>
</ul>
</li>
</ul>