GetPage
语法
PAGE.GetPage PATH
返回值
page.Page
GetPage
方法也可在 Site
对象上使用。请参阅详情。
当在 Page
对象上使用 GetPage
方法时,请指定相对于当前目录或相对于 content
目录的路径。
如果 Hugo 无法将路径解析到页面,则该方法返回 nil。如果路径不明确,Hugo 会抛出错误并导致构建失败。
考虑以下内容结构
content/
├── works/
│ ├── paintings/
│ │ ├── _index.md
│ │ ├── starry-night.md
│ │ └── the-mona-lisa.md
│ ├── sculptures/
│ │ ├── _index.md
│ │ ├── david.md
│ │ └── the-thinker.md
│ └── _index.md
└── _index.md
以下示例描述了渲染 works/paintings/the-mona-lisa.md 的结果
layouts/works/single.html
{{ with .GetPage "starry-night" }}
{{ .Title }} → Starry Night
{{ end }}
{{ with .GetPage "./starry-night" }}
{{ .Title }} → Starry Night
{{ end }}
{{ with .GetPage "../paintings/starry-night" }}
{{ .Title }} → Starry Night
{{ end }}
{{ with .GetPage "/works/paintings/starry-night" }}
{{ .Title }} → Starry Night
{{ end }}
{{ with .GetPage "../sculptures/david" }}
{{ .Title }} → David
{{ end }}
{{ with .GetPage "/works/sculptures/david" }}
{{ .Title }} → David
{{ end }}