HasChildren
Syntax
MENUENTRY.HasChildren
Returns
bool
在渲染嵌套菜单时,请使用HasChildren
方法。
使用以下站点配置:
hugo.
menu:
main:
- name: Products
pageRef: /product
weight: 10
- name: Product 1
pageRef: /products/product-1
parent: Products
weight: 1
- name: Product 2
pageRef: /products/product-2
parent: Products
weight: 2
[menu]
[[menu.main]]
name = 'Products'
pageRef = '/product'
weight = 10
[[menu.main]]
name = 'Product 1'
pageRef = '/products/product-1'
parent = 'Products'
weight = 1
[[menu.main]]
name = 'Product 2'
pageRef = '/products/product-2'
parent = 'Products'
weight = 2
{
"menu": {
"main": [
{
"name": "Products",
"pageRef": "/product",
"weight": 10
},
{
"name": "Product 1",
"pageRef": "/products/product-1",
"parent": "Products",
"weight": 1
},
{
"name": "Product 2",
"pageRef": "/products/product-2",
"parent": "Products",
"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/">Products</a>
<ul>
<li><a href="/products/product-1/">Product 1</a></li>
<li><a href="/products/product-2/">Product 2</a></li>
</ul>
</li>
</ul>