diff --git a/content/en/methods/menu-entry/PageRef.md b/content/en/methods/menu-entry/PageRef.md new file mode 100644 index 000000000..5d99d628c --- /dev/null +++ b/content/en/methods/menu-entry/PageRef.md @@ -0,0 +1,120 @@ +--- +title: PageRef +description: Returns the `pageRef` property of the given menu entry. +categories: [] +keywords: [] +action: + related: + - /methods/menu-entry/URL + returnType: string + signatures: [MENUENTRY.PageRef] +toc: true +--- + +The use case for this method is rare. + +In almost also scenarios you should use the [`URL`] method instead. + +## Explanation + +If you specify a `pageRef` property when [defining a menu entry] in your site configuration, Hugo looks for a matching page when rendering the entry. + +If a matching page is found: + +- The [`URL`] method returns the page's relative permalink +- The [`Page`] method returns the corresponding `Page` object +- The [`HasMenuCurrent`] and [`IsMenuCurrent`] methods on a `Page` object return the expected values + +If a matching page is not found: + +- The [`URL`] method returns the entry's `url` property if set, else an empty string +- The [`Page`] method returns nil +- The [`HasMenuCurrent`] and [`IsMenuCurrent`] methods on a `Page` object return `false` + +{{% note %}} +In almost also scenarios you should use the [`URL`] method instead. + +[`URL`]: /methods/menu-entry/url/ +{{% /note %}} + +[defining a menu entry]: /content-management/menus/#define-in-site-configuration +[`Page`]: /methods/menu-entry/page/ +[`URL`]: /methods/menu-entry/url/ +[`IsMenuCurrent`]: /methods/page/ismenucurrent/ +[`HasMenuCurrent`]: /methods/page/hasmenucurrent/ +[`RelPermalink`]: /methods/page/relpermalink/ + +## Example + +This example is contrived. + +{{% note %}} +In almost also scenarios you should use the [`URL`] method instead. + +[`URL`]: /methods/menu-entry/url/ +{{% /note %}} + + +Consider this content structure: + +```text +content/ +├── products.md +└── _index.md +``` + +And this menu definition: + +{{< code-toggle file=hugo >}} +[[menus.main]] +name = 'Products' +pageRef = '/products' +weight = 10 +[[menus.main]] +name = 'Services' +pageRef = '/services' +weight = 20 +{{< /code-toggle >}} + +With this template code: + +{{< code file=layouts/partials/menu.html >}} + +{{< /code >}} + +Hugo render this HTML: + +```html + +``` + +In the above note that the `href` attribute of the second `anchor` element is blank because Hugo was unable to find the "services" page. + +With this template code: + + +{{< code file=layouts/partials/menu.html >}} + +{{< /code >}} + +Hugo renders this HTML: + +```html + +``` + +In the above note that Hugo populates the `href` attribute of the second `anchor` element with the `pageRef` property as defined in the site configuration because the template code falls back to the `PageRef` method. diff --git a/content/en/methods/page/HasMenuCurrent.md b/content/en/methods/page/HasMenuCurrent.md index a092f22c0..24ce462c9 100644 --- a/content/en/methods/page/HasMenuCurrent.md +++ b/content/en/methods/page/HasMenuCurrent.md @@ -28,4 +28,8 @@ If the `Page` object associated with the menu entry is a section, this method al See [menu templates] for a complete example. +{{% note %}} +When using this method you must either define the menu entry in front matter, or specify a `pageRef` property when defining the menu entry in your site configuration. +{{% /note %}} + [menu templates]: /templates/menu/#example diff --git a/content/en/methods/page/IsMenuCurrent.md b/content/en/methods/page/IsMenuCurrent.md index 8a939b1a1..a16be51ee 100644 --- a/content/en/methods/page/IsMenuCurrent.md +++ b/content/en/methods/page/IsMenuCurrent.md @@ -26,4 +26,8 @@ aliases: [/functions/ismenucurrent] See [menu templates] for a complete example. +{{% note %}} +When using this method you must either define the menu entry in front matter, or specify a `pageRef` property when defining the menu entry in your site configuration. +{{% /note %}} + [menu templates]: /templates/menu/#example