HUGO
Menu
GitHub 90451 stars Mastodon

GetPage

Returns a Page object from the given path.
Syntax
SITE.GetPage PATH
Returns
page.Page

Usage

The GetPage method on a Site object returns the page with the given logical path, relative to the content directory. The GetPage method on a Page object also accepts a path relative to the current page.

If the path does not contain a slash and Hugo has not found the page, the method searches for a page with the same base name. If more than one page has the same base name, Hugo throws an error and fails the build.

If the path resolution rules do not find a page, the method returns nil.

With multilingual projects, the method returns the page in the current language. To get a page in a different language, query the Sites object as shown below.

Examples

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:

layouts/home.html
{{ 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>

Getting a regular page instead of a section page:

layouts/home.html
{{ with site.GetPage "/works/paintings/starry-night" }}
  {{ .Title }} → Starry Night
  {{ .Params.artist }} → Vincent van Gogh
{{ end }}

Getting a page in a different language by querying the Sites object:

{{ with where hugo.Sites "Language.Name" "eq" "de" }}
  {{ with index . 0 }}
    {{ with .GetPage "/works/paintings/starry-night" }}
      {{ .Title }} → Sternenklare Nacht
    {{ end }}
  {{ end }}
{{ end }}

Rendering the images in a headless bundle at content/headless/index.md:

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