HUGO
Menu
GitHub 87548 stars Mastodon

Page

返回与给定菜单项关联的页面对象。

Syntax

MENUENTRY.Page

Returns

page.Page

无论您如何 定义菜单条目,与页面关联的条目都可以访问其 方法

在此菜单定义中,前两个条目与页面关联,最后一个条目不关联:

menus:
  main:
  - pageRef: /about
    weight: 10
  - pageRef: /contact
    weight: 20
  - name: Hugo
    url: https://www.hugodoc.com
    weight: 30
[menus]
  [[menus.main]]
    pageRef = '/about'
    weight = 10
  [[menus.main]]
    pageRef = '/contact'
    weight = 20
  [[menus.main]]
    name = 'Hugo'
    url = 'https://www.hugodoc.com'
    weight = 30
{
   "menus": {
      "main": [
         {
            "pageRef": "/about",
            "weight": 10
         },
         {
            "pageRef": "/contact",
            "weight": 20
         },
         {
            "name": "Hugo",
            "url": "https://www.hugodoc.com",
            "weight": 30
         }
      ]
   }
}

在此示例中,如果菜单条目与页面关联,我们在渲染锚元素时使用页面的 RelPermalinkLinkTitle

如果条目未与页面关联,我们使用其 urlname 属性。

<ul>
  {{ range .Site.Menus.main }}
    {{ with .Page }}
      <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
    {{ else }}
      <li><a href="{{ .URL }}">{{ .Name }}</a></li>
    {{ end }}
  {{ end }}
</ul>

有关更多信息,请参阅 菜单模板 部分。


Last updated: January 1, 0001
Improve this page