NextInSection
语法
PAGE.NextInSection
返回值
page.Page
Hugo 通过按照此排序层次结构对当前章节的常规页面进行排序来确定下一个和上一个页面
字段 | 优先级 | 排序方向 |
---|---|---|
权重 |
1 | 降序 |
日期 |
2 | 降序 |
链接标题 |
3 | 降序 |
path |
4 | 降序 |
用于确定下一个和上一个页面的已排序页面集合独立于其他页面集合,这可能会导致意外行为。
例如,对于此内容结构
content/
├── pages/
│ ├── _index.md
│ ├── page-1.md <-- front matter: weight = 10
│ ├── page-2.md <-- front matter: weight = 20
│ └── page-3.md <-- front matter: weight = 30
└── _index.md
以及这些模板
layouts/_default/list.html
{{ range .Pages.ByWeight }}
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
{{ end }}
layouts/_default/single.html
{{ with .PrevInSection }}
<a href="{{ .RelPermalink }}">Previous</a>
{{ end }}
{{ with .NextInSection }}
<a href="{{ .RelPermalink }}">Next</a>
{{ end }}
当您访问 page-2 时
PrevInSection
方法指向 page-3NextInSection
方法指向 page-1
要反转下一个和上一个的含义,您可以在站点配置中更改排序方向,或者在 Pages
对象上使用 Next
和 Prev
方法以获得更大的灵活性。
示例
通过检查页面是否存在来进行防御性编码
{{ with .PrevInSection }}
<a href="{{ .RelPermalink }}">Previous</a>
{{ end }}
{{ with .NextInSection }}
<a href="{{ .RelPermalink }}">Next</a>
{{ end }}