hugo/docs/content/en/methods/site/GetPage.md
Bjørn Erik Pedersen 5fd1e74903
Merge commit '9b0050e9aabe4be65c78ccf292a348f309d50ccd' as 'docs'
```
git subtree add --prefix=docs/ https://github.com/gohugoio/hugoDocs.git master --squash
```

Closes #11925
2024-01-27 10:48:57 +01:00

2.5 KiB

title description categories keywords action toc
GetPage Returns a Page object from the given path.
related returnType signatures
methods/page/GetPage
page.Page
SITE.GetPage PATH
true

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

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:

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:

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

Is rendered to:

<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:

{{ 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:

{{ 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:

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:

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