Debian Codegraph @

docs/content/en/methods/site/GetPage.md

raw
1 ---
2 title: GetPage
3 description: Returns a Page object from the given path.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 returnType: page.Page
9 signatures: [SITE.GetPage PATH]
10 ---
11
12 The `GetPage` method is also available on `Page` objects, allowing you to specify a path relative to the current page. See [details][].
13
14 When using the `GetPage` method on a `Site` object, specify a path relative to the `content` directory.
15
16 If Hugo cannot resolve the path to a page, the method returns nil.
17
18 Consider this content structure:
19
20 ```tree
21 content/
22 ├── works/
23 │ ├── paintings/
24 │ │ ├── _index.md
25 │ │ ├── starry-night.md
26 │ │ └── the-mona-lisa.md
27 │ ├── sculptures/
28 │ │ ├── _index.md
29 │ │ ├── david.md
30 │ │ └── the-thinker.md
31 │ └── _index.md
32 └── _index.md
33 ```
34
35 This _home_ template:
36
37 ```go-html-template {file="layouts/home.html"}
38 {{ with .Site.GetPage "/works/paintings" }}
39 <ul>
40 {{ range .Pages }}
41 <li>{{ .Title }} by {{ .Params.artist }}</li>
42 {{ end }}
43 </ul>
44 {{ end }}
45 ```
46
47 Is rendered to:
48
49 ```html
50 <ul>
51 <li>Starry Night by Vincent van Gogh</li>
52 <li>The Mona Lisa by Leonardo da Vinci</li>
53 </ul>
54 ```
55
56 To get a regular page instead of a section page:
57
58 ```go-html-template {file="layouts/home.html"}
59 {{ with .Site.GetPage "/works/paintings/starry-night" }}
60 {{ .Title }} → Starry Night
61 {{ .Params.artist }} → Vincent van Gogh
62 {{ end }}
63 ```
64
65 ## Multilingual projects
66
67 With multilingual projects, the `GetPage` method on a `Site` object resolves the given path to a page in the current language.
68
69 To get a page from a different language, query the `Sites` object:
70
71 ```go-html-template
72 {{ with where hugo.Sites "Language.Name" "eq" "de" }}
73 {{ with index . 0 }}
74 {{ with .GetPage "/works/paintings/starry-night" }}
75 {{ .Title }} → Sternenklare Nacht
76 {{ end }}
77 {{ end }}
78 {{ end }}
79 ```
80
81 ## Page bundles
82
83 Consider this content structure:
84
85 ```tree
86 content/
87 ├── headless/
88 │ ├── a.jpg
89 │ ├── b.jpg
90 │ ├── c.jpg
91 │ └── index.md <-- front matter: headless = true
92 └── _index.md
93 ```
94
95 In the _home_ template, use the `GetPage` method on a `Site` object to render all the images in the headless [page bundle](g):
96
97 ```go-html-template {file="layouts/home.html"}
98 {{ with .Site.GetPage "/headless" }}
99 {{ range .Resources.ByType "image" }}
100 <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
101 {{ end }}
102 {{ end }}
103 ```
104
105 [details]: /methods/page/getpage/