HUGO
Menu
GitHub 87548 stars Mastodon

KeyName

返回給定菜單項的 identifier 屬性,回退到其 name 屬性。

Syntax

MENUENTRY.KeyName

Returns

string

在此菜單定義中,第二個條目不包含 identifier,因此 Identifier 方法返回其 name 屬性:

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
         }
      ]
   }
}

此示例在多語言站點上使用 KeyName 方法查詢翻譯表,如果翻譯表中不存在匹配的鍵,則回退到 name 屬性:

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

在上面的示例中,我們需要將通過 .KeyName 返回的值傳遞給 lower 函數,因為翻譯表中的鍵是小寫的。


Last updated: January 1, 0001
Improve this page