collections.After

以下範例展示了將 afterslice 函數結合使用的情況:

{{ $data := slice "one" "two" "three" "four" }}
<ul>
  {{ range after 2 $data }}
    <li>{{ . }}</li>
  {{ end }}
</ul>

上述範本將被渲染為:

<ul>
  <li>three</li>
  <li>four</li>
</ul>

使用 afterfirst 的範例:第 2 到第 4 最新的文章

你可以將 afterfirst 函數及 Hugo 的強大排序方法結合使用。假設你在 example.com/articles 上有一個 section 頁面,並且有 10 篇文章,但你的範本只需要顯示兩個區塊:

  1. 第一個區塊標題為 “Featured”,僅顯示最新發布的文章(即根據內容檔案 Front Matter 中的 publishdate)。
  2. 第二個區塊標題為 “Recent Articles”,僅顯示第 2 到第 4 最新發布的文章。
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 }}