# KeyName
> 返回给定菜单条目的 `identifier` 属性，若不存在则回退到其 `name` 属性。
- 官方英文原文：https://gohugo.io/methods/menu-entry/keyname/
- 本页规范地址：https://hugozh.cn/methods/menu-entry/keyname/
- 最近更新：2026-10-02
- 最后提交：912b1d3 chore(site): 添加 static/CNAME（hugozh.cn），供 GitHub Pages 等平台绑定自定义域名
- 签名：MENUENTRY.KeyName
- 返回类型：string
- 站点：Hugo 中文文档（https://hugozh.cn/）· 社区维护的非官方中文翻译，如有出入以官方英文原文为准

---
在下面的菜单定义中，第二个条目没有 `identifier`，因此 `Identifier` 方法改为返回其 `name` 属性：

```toml
[[menus.main]]
identifier = 'about'
name = 'About'
pageRef = '/about'
weight = 10

[[menus.main]]
name = 'Contact'
pageRef = '/contact'
weight = 20
```

下面的示例在多语言项目中查询翻译表时使用了 `KeyName` 方法，若翻译表中不存在匹配的键，则回退到 `name` 属性：

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

在上面的示例中，需要把 `.KeyName` 返回的值传给 [`strings.ToLower`][] 函数，因为翻译表中的键是小写的。

[`strings.ToLower`]: /functions/strings/tolower/

