1
0
Fork 0
hugo/docs/content/en/methods/site/GetPage.md

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