| 1 |
--- |
| 2 |
title: KeyName |
| 3 |
description: Returns the `identifier` property of the given menu entry, falling back to its `name` property. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [MENUENTRY.KeyName] |
| 10 |
--- |
| 11 |
|
| 12 |
In this menu definition, the second entry does not contain an `identifier`, so the `Identifier` method returns its `name` property instead: |
| 13 |
|
| 14 |
{{< code-toggle file=hugo >}} |
| 15 |
[[menus.main]] |
| 16 |
identifier = 'about' |
| 17 |
name = 'About' |
| 18 |
pageRef = '/about' |
| 19 |
weight = 10 |
| 20 |
|
| 21 |
[[menus.main]] |
| 22 |
name = 'Contact' |
| 23 |
pageRef = '/contact' |
| 24 |
weight = 20 |
| 25 |
{{< /code-toggle >}} |
| 26 |
|
| 27 |
This example uses the `KeyName` method when querying the translation table on a multilingual project, falling back the `name` property if a matching key in the translation table does not exist: |
| 28 |
|
| 29 |
```go-html-template |
| 30 |
<ul> |
| 31 |
{{ range .Site.Menus.main }} |
| 32 |
<li><a href="{{ .URL }}">{{ or (T (.KeyName | lower)) .Name }}</a></li> |
| 33 |
{{ end }} |
| 34 |
</ul> |
| 35 |
``` |
| 36 |
|
| 37 |
In the example above, we need to pass the value returned by `.KeyName` through the [`strings.ToLower`][] function because the keys in the translation table are lowercase. |
| 38 |
|
| 39 |
[`strings.ToLower`]: /functions/strings/tolower/ |