Page

Page


Returns the Page object associated with the given menu entry.返回与给定菜单项关联的 Page 对象。

Regardless of how you [define menu entries], an entry associated with a page has access to its methods.

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

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

In this menu definition, the first two entries are associated with a page, the last entry is not:

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

In this example, if the menu entry is associated with a page, we use page’s RelPermalink and LinkTitle when rendering the anchor element.

If the entry is not associated with a page, we use its url and name properties.

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

See the menu templates section for more information.