| 1 |
--- |
| 2 |
title: HasChildren |
| 3 |
description: Reports whether the given menu entry has child menu entries. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: bool |
| 9 |
signatures: [MENUENTRY.HasChildren] |
| 10 |
--- |
| 11 |
|
| 12 |
Use the `HasChildren` method when rendering a nested menu. |
| 13 |
|
| 14 |
With this project configuration: |
| 15 |
|
| 16 |
{{< code-toggle file=hugo >}} |
| 17 |
[[menus.main]] |
| 18 |
name = 'Products' |
| 19 |
pageRef = '/product' |
| 20 |
weight = 10 |
| 21 |
|
| 22 |
[[menus.main]] |
| 23 |
name = 'Product 1' |
| 24 |
pageRef = '/products/product-1' |
| 25 |
parent = 'Products' |
| 26 |
weight = 1 |
| 27 |
|
| 28 |
[[menus.main]] |
| 29 |
name = 'Product 2' |
| 30 |
pageRef = '/products/product-2' |
| 31 |
parent = 'Products' |
| 32 |
weight = 2 |
| 33 |
{{< /code-toggle >}} |
| 34 |
|
| 35 |
And this template: |
| 36 |
|
| 37 |
```go-html-template |
| 38 |
<ul> |
| 39 |
{{ range .Site.Menus.main }} |
| 40 |
<li> |
| 41 |
<a href="{{ .URL }}">{{ .Name }}</a> |
| 42 |
{{ if .HasChildren }} |
| 43 |
<ul> |
| 44 |
{{ range .Children }} |
| 45 |
<li><a href="{{ .URL }}">{{ .Name }}</a></li> |
| 46 |
{{ end }} |
| 47 |
</ul> |
| 48 |
{{ end }} |
| 49 |
</li> |
| 50 |
{{ end }} |
| 51 |
</ul> |
| 52 |
``` |
| 53 |
|
| 54 |
Hugo renders this HTML: |
| 55 |
|
| 56 |
```html |
| 57 |
<ul> |
| 58 |
<li> |
| 59 |
<a href="/products/">Products</a> |
| 60 |
<ul> |
| 61 |
<li><a href="/products/product-1/">Product 1</a></li> |
| 62 |
<li><a href="/products/product-2/">Product 2</a></li> |
| 63 |
</ul> |
| 64 |
</li> |
| 65 |
</ul> |
| 66 |
``` |