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