GetPage

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 }}