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