after.md (2091B)
1 ---
2 title: after
3 description: "`after` slices an array to only the items after the <em>N</em>th item."
4 date: 2017-02-01
5 publishdate: 2017-02-01
6 lastmod: 2017-02-01
7 categories: [functions]
8 menu:
9 docs:
10 parent: "functions"
11 keywords: [iteration]
12 signature: ["after INDEX COLLECTION"]
13 workson: []
14 hugoversion:
15 relatedfuncs: [last,first,seq]
16 deprecated: false
17 aliases: []
18 ---
19
20 The following shows `after` being used in conjunction with the [`slice` function][slice]:
21
22 ```
23 {{ $data := slice "one" "two" "three" "four" }}
24 {{ range after 2 $data }}
25 {{ . }}
26 {{ end }}
27 → ["three", "four"]
28 ```
29
30 ## Example of `after` with `first`: 2nd–4th Most Recent Articles
31
32 You can use `after` in combination with the [`first` function][] and Hugo's [powerful sorting methods][lists]. Let's assume you have a list page at `example.com/articles`. You have 10 articles, but you want your templating for the [list/section page][] to show only two rows:
33
34 1. The top row is titled "Featured" and shows only the most recently published article (i.e. by `publishdate` in the content files' front matter).
35 2. The second row is titled "Recent Articles" and shows only the 2nd- to 4th-most recently published articles.
36
37 {{< code file="layouts/section/articles.html" download="articles.html" >}}
38 {{ define "main" }}
39 <section class="row featured-article">
40 <h2>Featured Article</h2>
41 {{ range first 1 .Pages.ByPublishDate.Reverse }}
42 <header>
43 <h3><a href="{{.Permalink}}">{{.Title}}</a></h3>
44 </header>
45 <p>{{.Description}}</p>
46 {{ end }}
47 </section>
48 <div class="row recent-articles">
49 <h2>Recent Articles</h2>
50 {{ range first 3 (after 1 .Pages.ByPublishDate.Reverse) }}
51 <section class="recent-article">
52 <header>
53 <h3><a href="{{.Permalink}}">{{.Title}}</a></h3>
54 </header>
55 <p>{{.Description}}</p>
56 </section>
57 {{ end }}
58 </div>
59 {{ end }}
60 {{< /code >}}
61
62 [`first` function]: /functions/first/
63 [list/section page]: /templates/section-templates/
64 [lists]: /templates/lists/#order-content
65 [slice]: /functions/slice/