hugo/docs/content/en/methods/menu-entry/Page.md
2023-12-04 15:24:01 +01:00

1.3 KiB

title description categories keywords action
Page Returns the Page object associated with the given menu entry.
related returnType signatures
hugolib.pageState
MENUENTRY.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:

{{< code-toggle file=hugo >}} menu.main pageRef = '/about' weight = 10

menu.main pageRef = '/contact' weight = 20

menu.main name = 'Hugo' url = 'https://gohugo.io' weight = 30 {{< /code-toggle >}}

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.