| 1 |
--- |
| 2 |
title: page |
| 3 |
description: Provides global access to a Page object. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: |
| 10 |
signatures: [page] |
| 11 |
aliases: [/functions/page] |
| 12 |
--- |
| 13 |
|
| 14 |
At the top level of a template that receives a `Page` object in context, these are equivalent: |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ .Params.foo }} |
| 18 |
{{ .Page.Params.foo }} |
| 19 |
{{ page.Params.foo }} |
| 20 |
``` |
| 21 |
|
| 22 |
When a `Page` object is not in context, you can use the global `page` function: |
| 23 |
|
| 24 |
```go-html-template |
| 25 |
{{ page.Params.foo }} |
| 26 |
``` |
| 27 |
|
| 28 |
> [!NOTE] |
| 29 |
> Do not use the global `page` function in shortcodes, _partial_ templates called by shortcodes, or cached _partial_ templates. See [warnings](#warnings) below. |
| 30 |
|
| 31 |
## Explanation |
| 32 |
|
| 33 |
Hugo almost always passes a `Page` as the data context into the top-level template (e.g., `baseof.html`). The one exception is the multihost sitemap template. This means that you can access the current page with the `.` in the template. |
| 34 |
|
| 35 |
However, when deeply nested inside a [partial template](g) or [render hook](g), it is not always practical or possible to access the `Page` object. |
| 36 |
|
| 37 |
Use the global `page` function to access the `Page` object from anywhere in any template. |
| 38 |
|
| 39 |
## Warnings |
| 40 |
|
| 41 |
### Be aware of top-level context |
| 42 |
|
| 43 |
The global `page` function accesses the `Page` object passed into the top-level template. |
| 44 |
|
| 45 |
With this content structure: |
| 46 |
|
| 47 |
```tree |
| 48 |
content/ |
| 49 |
├── posts/ |
| 50 |
│ ├── post-1.md |
| 51 |
│ ├── post-2.md |
| 52 |
│ └── post-3.md |
| 53 |
└── _index.md <-- title is "My Home Page" |
| 54 |
``` |
| 55 |
|
| 56 |
And this code in the _home_ template: |
| 57 |
|
| 58 |
```go-html-template {file="layouts/home.html"} |
| 59 |
{{ range site.Sections }} |
| 60 |
{{ range .Pages }} |
| 61 |
{{ page.Title }} |
| 62 |
{{ end }} |
| 63 |
{{ end }} |
| 64 |
``` |
| 65 |
|
| 66 |
The rendered output will be: |
| 67 |
|
| 68 |
```text |
| 69 |
My Home Page |
| 70 |
My Home Page |
| 71 |
My Home Page |
| 72 |
``` |
| 73 |
|
| 74 |
In the example above, the global `page` function accesses the `Page` object passed into the _home_ template; it does not access the `Page` object of the iterated pages. |
| 75 |
|
| 76 |
### Be aware of caching |
| 77 |
|
| 78 |
Do not use the global `page` function in: |
| 79 |
|
| 80 |
- Shortcodes |
| 81 |
- _Partial_ templates called by shortcodes |
| 82 |
- _Partial_ templates cached by the [`partialCached`][] function |
| 83 |
|
| 84 |
Hugo caches rendered shortcodes. If you use the global `page` function within a shortcode, and the page content is rendered in two or more templates, the cached shortcode may be incorrect. |
| 85 |
|
| 86 |
Consider this _section_ template: |
| 87 |
|
| 88 |
```go-html-template {file="layouts/section.html"} |
| 89 |
{{ range .Pages }} |
| 90 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 91 |
{{ .Summary }} |
| 92 |
{{ end }} |
| 93 |
``` |
| 94 |
|
| 95 |
When you call the [`Summary`][] method, Hugo renders the page content including shortcodes. In this case, within a shortcode, the global `page` function accesses the `Page` object of the section page, not the content page. |
| 96 |
|
| 97 |
If Hugo renders the section page before a content page, the cached rendered shortcode will be incorrect. You cannot control the rendering sequence due to concurrency. |
| 98 |
|
| 99 |
[`Summary`]: /methods/page/summary/ |
| 100 |
[`partialCached`]: /functions/partials/includecached/ |