Debian Codegraph @

docs/content/en/functions/global/page.md

raw
1 ---
2 title: page
3 description: Returns the Page object of the current page, accessible from any context.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: page.Page
10 signatures: [page]
11 aliases: [/functions/page]
12 ---
13
14 ## Usage
15
16 At the top level of a template that receives a `Page` object in context, these are equivalent:
17
18 ```go-html-template
19 {{ .Params.foo }}
20 {{ .Page.Params.foo }}
21 {{ page.Params.foo }}
22 ```
23
24 When a `Page` object is not in context, you can use the global `page` function:
25
26 ```go-html-template
27 {{ page.Params.foo }}
28 ```
29
30 > [!NOTE]
31 > Do not use the global `page` function in shortcodes, _partial_ templates called by shortcodes, or cached _partial_ templates. See [examples](#examples) below.
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 ## Examples
40
41 The following examples demonstrate common pitfalls when using the global `page` function.
42
43 ### Be aware of top-level context
44
45 The global `page` function accesses the `Page` object passed into the top-level template.
46
47 With this content structure:
48
49 ```tree
50 content/
51 ├── posts/
52 │ ├── post-1.md
53 │ ├── post-2.md
54 │ └── post-3.md
55 └── _index.md <-- title is "My Home Page"
56 ```
57
58 And this code in the _home_ template:
59
60 ```go-html-template {file="layouts/home.html"}
61 {{ range site.Sections }}
62 {{ range .Pages }}
63 {{ page.Title }}
64 {{ end }}
65 {{ end }}
66 ```
67
68 The rendered output will be:
69
70 ```text
71 My Home Page
72 My Home Page
73 My Home Page
74 ```
75
76 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.
77
78 ### Be aware of caching
79
80 Do not use the global `page` function in:
81
82 - Shortcodes
83 - _Partial_ templates called by shortcodes
84 - _Partial_ templates cached by the [`partialCached`][] function
85
86 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.
87
88 Consider this _section_ template:
89
90 ```go-html-template {file="layouts/section.html"}
91 {{ range .Pages }}
92 <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
93 {{ .Summary }}
94 {{ end }}
95 ```
96
97 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.
98
99 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.
100
101 [`Summary`]: /methods/page/summary/
102 [`partialCached`]: /functions/partials/includecached/