hugo/docs/content/en/methods/menu-entry/KeyName.md
2023-12-04 15:24:01 +01:00

1.1 KiB

title description categories keywords action
KeyName Returns the `identifier` property of the given menu entry, falling back to its `name` property.
related returnType signatures
string
MENUENTRY.KeyName

In this menu definition, the second entry does not contain an identifier, so the Identifier method returns its name property instead:

{{< code-toggle file=hugo >}} menu.main identifier = 'about' name = 'About' pageRef = '/about' weight = 10

menu.main name = 'Contact' pageRef = '/contact' weight = 20 {{< /code-toggle >}}

This example uses the KeyName method when querying the translation table on a multilingual site, falling back the name property if a matching key in the translation table does not exist:

<ul>
  {{ range .Site.Menus.main }}
    <li><a href="{{ .URL }}">{{ or (T (.KeyName | lower)) .Name }}</a></li>
  {{ end }}
</ul>

In the example above, we need to pass the value returned by .KeyName through the lower function because the keys in the translation table are lowercase.