GetPage
Syntax
PAGE.GetPage PATH
Returns
page.Page
GetPage
方法也适用于 Site
对象。详情见 详情 。
在 Page
对象上使用 GetPage
方法时,请指定相对于当前目录或内容目录的路径。
如果 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 }}