aboutsummaryrefslogtreecommitdiffhomepage
path: root/docs/content/en/methods/site/GetPage.md
blob: b7d4b8f32f593ed8a43949f0483536dddf3266c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
title: GetPage
description: Returns a Page object from the given path.
categories: []
keywords: []
action:
  related:
    - methods/page/GetPage
  returnType: page.Page
  signatures: [SITE.GetPage PATH]
toc: true
---

The `GetPage` method is also available on `Page` objects, allowing you to specify a path relative to the current page. See [details].

[details]: /methods/page/getpage

When using the `GetPage` method on a `Site` object, specify a path relative to the content directory.

If Hugo cannot resolve the path to a page, the method returns nil.

Consider this content structure:

```text
content/
├── works/
│   ├── paintings/
│   │   ├── _index.md
│   │   ├── starry-night.md
│   │   └── the-mona-lisa.md
│   ├── sculptures/
│   │   ├── _index.md
│   │   ├── david.md
│   │   └── the-thinker.md
│   └── _index.md
└── _index.md
```

This home page template:

```go-html-template
{{ with .Site.GetPage "/works/paintings" }}
  <ul>
    {{ range .Pages }}
      <li>{{ .Title }} by {{ .Params.artist }}</li>
    {{ end }}
  </ul>
{{ end }}
```

Is rendered to:

```html
<ul>
  <li>Starry Night by Vincent van Gogh</li>
  <li>The Mona Lisa by Leonardo da Vinci</li>
</ul>
```

To get a regular page instead of a section page:

```go-html-template
{{ with .Site.GetPage "/works/paintings/starry-night" }}
  {{ .Title }} → Starry Night
  {{ .Params.artist }} → Vincent van Gogh
{{ end }}
```

## Multilingual projects

With multilingual projects, the `GetPage` method on a `Site` object resolves the given path to a page in the current language.

To get a page from a different language, query the `Sites` object:

```go-html-template
{{ with where .Site.Sites "Language.Lang" "eq" "de" }}
  {{ with index . 0 }}
    {{ with .GetPage "/works/paintings/starry-night" }}
      {{ .Title }} → Sternenklare Nacht
    {{ end }}
  {{ end }}
{{ end }}
```

## Page bundles

Consider this content structure:

```text
content/
├── headless/    
│   ├── a.jpg
│   ├── b.jpg
│   ├── c.jpg
│   └── index.md  <-- front matter: headless = true
└── _index.md
```

In the home page template, use the `GetPage` method on a `Site` object to render all the images in the headless [page bundle]:

```go-html-template
{{ with .Site.GetPage "/headless" }}
  {{ range .Resources.ByType "image" }}
    <img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt="">
  {{ end }}
{{ end }}
```

[page bundle]: /getting-started/glossary/#page-bundle