Identifier
返回給定菜單項的
identifier 屬性。
Syntax
MENUENTRY.Identifier
Returns
string
Identifier 方法返回菜單項的 identifier 屬性。如果您是 自動 定義菜單項,它返回頁面的 section。
menus:
main:
- identifier: about
name: About
pageRef: /about
weight: 10
- identifier: contact
name: Contact
pageRef: /contact
weight: 20
[menus]
[[menus.main]]
identifier = 'about'
name = 'About'
pageRef = '/about'
weight = 10
[[menus.main]]
identifier = 'contact'
name = 'Contact'
pageRef = '/contact'
weight = 20
{
"menus": {
"main": [
{
"identifier": "about",
"name": "About",
"pageRef": "/about",
"weight": 10
},
{
"identifier": "contact",
"name": "Contact",
"pageRef": "/contact",
"weight": 20
}
]
}
}
此示例在多語言站點上使用 Identifier 方法查詢翻譯表,如果翻譯表中不存在匹配的鍵,則回退到 name 屬性:
<ul>
{{ range .Site.Menus.main }}
<li><a href="{{ .URL }}">{{ or (T .Identifier) .Name }}</a></li>
{{ end }}
</ul>在上面的菜單定義中,請注意當兩個或更多菜單條目具有相同的名稱,或使用翻譯表本地化名稱時,identifier 屬性才是必需的。