collections.After
返回给定切片中位于前 N 个元素之后的元素所组成的切片。
基本用法
下面演示 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>与 first 配合使用
你可以把 after 与 first 函数以及 Hugo 强大的排序方法组合使用。假设你在 example.org/articles 有一个 section 页面,共有 10 篇文章,但希望模板只显示两行:
- 第一行标题为 “Featured”,只显示最近发布的那篇文章(即按内容文件前置元数据中的
publishdate排序后的第一篇)。 - 第二行标题为 “Recent Articles”,只显示第 2 至第 4 篇最近发布的文章。
{{ 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 }}