| 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: [PAGE.GetPage PATH] |
| 10 |
aliases: [/functions/getpage] |
| 11 |
--- |
| 12 |
|
| 13 |
The `GetPage` method is also available on a `Site` object. See [details][]. |
| 14 |
|
| 15 |
When using the `GetPage` method on the `Page` object, specify a path relative to the current directory or relative to the `content` directory. |
| 16 |
|
| 17 |
If Hugo cannot resolve the path to a page, the method returns `nil`. If the path is ambiguous, Hugo throws an error and fails the build. |
| 18 |
|
| 19 |
Consider this content structure: |
| 20 |
|
| 21 |
```tree |
| 22 |
content/ |
| 23 |
├── works/ |
| 24 |
│ ├── paintings/ |
| 25 |
│ │ ├── _index.md |
| 26 |
│ │ ├── starry-night.md |
| 27 |
│ │ └── the-mona-lisa.md |
| 28 |
│ ├── sculptures/ |
| 29 |
│ │ ├── _index.md |
| 30 |
│ │ ├── david.md |
| 31 |
│ │ └── the-thinker.md |
| 32 |
│ └── _index.md |
| 33 |
└── _index.md |
| 34 |
``` |
| 35 |
|
| 36 |
The examples below depict the result of rendering `works/paintings/the-mona-lisa.md`: |
| 37 |
|
| 38 |
```go-html-template {file="layouts/works/page.html"} |
| 39 |
{{ with .GetPage "starry-night" }} |
| 40 |
{{ .Title }} → Starry Night |
| 41 |
{{ end }} |
| 42 |
|
| 43 |
{{ with .GetPage "./starry-night" }} |
| 44 |
{{ .Title }} → Starry Night |
| 45 |
{{ end }} |
| 46 |
|
| 47 |
{{ with .GetPage "../paintings/starry-night" }} |
| 48 |
{{ .Title }} → Starry Night |
| 49 |
{{ end }} |
| 50 |
|
| 51 |
{{ with .GetPage "/works/paintings/starry-night" }} |
| 52 |
{{ .Title }} → Starry Night |
| 53 |
{{ end }} |
| 54 |
|
| 55 |
{{ with .GetPage "../sculptures/david" }} |
| 56 |
{{ .Title }} → David |
| 57 |
{{ end }} |
| 58 |
|
| 59 |
{{ with .GetPage "/works/sculptures/david" }} |
| 60 |
{{ .Title }} → David |
| 61 |
{{ end }} |
| 62 |
``` |
| 63 |
|
| 64 |
[details]: /methods/site/getpage/ |