KeyName

KeyName


Returns the `identifier` property of the given menu entry, falling back to its `name` property. 返回identifier给定菜单项的属性,并恢复其name属性。

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

在此菜单定义中,第二个条目不包含identifier,因此该Identifier方法返回其name属性:

hugo.
     
menus:
  main:
  - identifier: about
    name: About
    pageRef: /about
    weight: 10
  - name: Contact
    pageRef: /contact
    weight: 20
[menus]
[[menus.main]]
    identifier = 'about'
    name = 'About'
    pageRef = '/about'
    weight = 10
[[menus.main]]
    name = 'Contact'
    pageRef = '/contact'
    weight = 20
{
   "menus": {
      "main": [
         {
            "identifier": "about",
            "name": "About",
            "pageRef": "/about",
            "weight": 10
         },
         {
            "name": "Contact",
            "pageRef": "/contact",
            "weight": 20
         }
      ]
   }
}

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.

在上面的例子中,我们需要.KeyName通过lower函数传递返回的值,因为翻译表中的键是小写的。