GetPage
根据给定路径返回 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 的结果:
{{ 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 }}