HUGO
Menu
GitHub 87548 stars Mastodon

MainSections

返回站點配置中定義的主節名稱切片,回退到具有最多頁面的頂級節。

Syntax

SITE.MainSections

Returns

[]string

站點配置:

mainSections:
- books
- films
mainSections = ['books', 'films']
{
   "mainSections": [
      "books",
      "films"
   ]
}

模板:

{{ .Site.MainSections }} → [books films]

如果站點配置中未定義 mainSections,此方法返回包含一個元素的切片——具有最多頁面的頂級節。

使用此內容結構,“films” 節具有最多頁面:

content/
├── books/
│   ├── book-1.md
│   └── book-2.md
├── films/
│   ├── film-1.md
│   ├── film-2.md
│   └── film-3.md
└── _index.md

模板:

{{ .Site.MainSections }} → [films]

創建主題時,不要在首頁列出最相關頁面時硬編碼節名稱,而是指示站點作者在其站點配置中設置 mainSections

然後您的 主頁 模板可以執行以下操作:

layouts/home.html
{{ range where .Site.RegularPages "Section" "in" .Site.MainSections }}
  <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}

Last updated: January 1, 0001
Improve this page