| 1 |
--- |
| 2 |
title: PageRef |
| 3 |
description: Returns the `pageRef` property of the given menu entry. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [MENUENTRY.PageRef] |
| 10 |
--- |
| 11 |
|
| 12 |
> [!NOTE] |
| 13 |
> The use case for this method is rare. |
| 14 |
> In almost also scenarios you should use the [`URL`][] method instead. |
| 15 |
|
| 16 |
## Explanation |
| 17 |
|
| 18 |
If you specify a `pageRef` property when [defining a menu entry][] in your project configuration, Hugo looks for a matching page when rendering the entry. |
| 19 |
|
| 20 |
If a matching page is found: |
| 21 |
|
| 22 |
- The [`URL`][] method returns the page's relative permalink |
| 23 |
- The [`Page`][] method returns the corresponding `Page` object |
| 24 |
- The [`HasMenuCurrent`][] and [`IsMenuCurrent`][] methods on a `Page` object return the expected values |
| 25 |
|
| 26 |
If a matching page is not found: |
| 27 |
|
| 28 |
- The [`URL`][] method returns the entry's `url` property if set, else an empty string |
| 29 |
- The [`Page`][] method returns `nil` |
| 30 |
- The [`HasMenuCurrent`][] and [`IsMenuCurrent`][] methods on a `Page` object return `false` |
| 31 |
|
| 32 |
> [!NOTE] |
| 33 |
> In almost also scenarios you should use the [`URL`][] method instead. |
| 34 |
|
| 35 |
## Example |
| 36 |
|
| 37 |
This example is contrived. |
| 38 |
|
| 39 |
> [!NOTE] |
| 40 |
> In almost also scenarios you should use the [`URL`][] method instead. |
| 41 |
|
| 42 |
Consider this content structure: |
| 43 |
|
| 44 |
```tree |
| 45 |
content/ |
| 46 |
├── products.md |
| 47 |
└── _index.md |
| 48 |
``` |
| 49 |
|
| 50 |
And this menu definition: |
| 51 |
|
| 52 |
{{< code-toggle file=hugo >}} |
| 53 |
[[menus.main]] |
| 54 |
name = 'Products' |
| 55 |
pageRef = '/products' |
| 56 |
weight = 10 |
| 57 |
[[menus.main]] |
| 58 |
name = 'Services' |
| 59 |
pageRef = '/services' |
| 60 |
weight = 20 |
| 61 |
{{< /code-toggle >}} |
| 62 |
|
| 63 |
With this template code: |
| 64 |
|
| 65 |
```go-html-template {file="layouts/_partials/menu.html"} |
| 66 |
<ul> |
| 67 |
{{ range .Site.Menus.main }} |
| 68 |
<li><a href="{{ .URL }}">{{ .Name }}</a></li> |
| 69 |
{{ end }} |
| 70 |
</ul> |
| 71 |
``` |
| 72 |
|
| 73 |
Hugo render this HTML: |
| 74 |
|
| 75 |
```html |
| 76 |
<ul> |
| 77 |
<li><a href="/products/">Products</a></li> |
| 78 |
<li><a href="">Services</a></li> |
| 79 |
</ul> |
| 80 |
``` |
| 81 |
|
| 82 |
In the above note that the `href` attribute of the second `anchor` element is blank because Hugo was unable to find the `services` page. |
| 83 |
|
| 84 |
With this template code: |
| 85 |
|
| 86 |
```go-html-template {file="layouts/_partials/menu.html"} |
| 87 |
<ul> |
| 88 |
{{ range .Site.Menus.main }} |
| 89 |
<li><a href="{{ or .URL .PageRef }}">{{ .Name }}</a></li> |
| 90 |
{{ end }} |
| 91 |
</ul> |
| 92 |
``` |
| 93 |
|
| 94 |
Hugo renders this HTML: |
| 95 |
|
| 96 |
```html |
| 97 |
<ul> |
| 98 |
<li><a href="/products/">Products</a></li> |
| 99 |
<li><a href="/services">Services</a></li> |
| 100 |
</ul> |
| 101 |
``` |
| 102 |
|
| 103 |
In the above note that Hugo populates the `href` attribute of the second `anchor` element with the `pageRef` property as defined in your project configuration because the template code falls back to the `PageRef` method. |
| 104 |
|
| 105 |
[`HasMenuCurrent`]: /methods/page/hasmenucurrent/ |
| 106 |
[`IsMenuCurrent`]: /methods/page/ismenucurrent/ |
| 107 |
[`Page`]: /methods/menu-entry/page/ |
| 108 |
[`URL`]: /methods/menu-entry/url/ |
| 109 |
[defining a menu entry]: /content-management/menus/#define-in-project-configuration |