collections.After
语法
collections.After INDEX COLLECTION
返回
any
别名
after
以下显示了 after
与 slice
函数结合使用的情况
{{ $data := slice "one" "two" "three" "four" }}
<ul>
{{ range after 2 $data }}
<li>{{ . }}</li>
{{ end }}
</ul>
上面的模板渲染为
<ul>
<li>three</li>
<li>four</li>
</ul>
after
与 first
结合使用的示例:第二到第四篇最新文章
您可以将 after
与 first
函数和 Hugo 的 强大的排序方法 结合使用。假设您在 example.com/articles
有一个 section
页面。您有 10 篇文章,但您希望您的模板只显示两行
- 顶行标题为“精选”,只显示最近发布的文章(即内容文件前言中的
publishdate
)。 - 第二行标题为“最近文章”,只显示第二到第四篇最近发布的文章。
layouts/section/articles.html
{{ define "main" }}
<section class="row featured-article">
<h2>Featured Article</h2>
{{ range first 1 .Pages.ByPublishDate.Reverse }}
<header>
<h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
</header>
<p>{{ .Description }}</p>
{{ end }}
</section>
<div class="row recent-articles">
<h2>Recent Articles</h2>
{{ range first 3 (after 1 .Pages.ByPublishDate.Reverse) }}
<section class="recent-article">
<header>
<h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
</header>
<p>{{ .Description }}</p>
</section>
{{ end }}
</div>
{{ end }}