Debian Codegraph @

docs/content/en/functions/collections/After.md

raw
1 ---
2 title: collections.After
3 description: Returns a slice containing the elements after the first N elements of the given slice.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: [after]
9 returnType: '[]any'
10 signatures: [collections.After N SLICE]
11 aliases: [/functions/after]
12 ---
13
14 The following shows `after` being used in conjunction with the [`slice`][] function:
15
16 ```go-html-template
17 {{ $data := slice "one" "two" "three" "four" }}
18 <ul>
19 {{ range after 2 $data }}
20 <li>{{ . }}</li>
21 {{ end }}
22 </ul>
23 ```
24
25 The template above is rendered to:
26
27 ```html
28 <ul>
29 <li>three</li>
30 <li>four</li>
31 </ul>
32 ```
33
34 ## Example of `after` with `first`: 2nd&ndash;4th most recent articles
35
36 You can use `after` in combination with the [`first`][] function and Hugo's [powerful sorting methods][]. Let's assume you have a `section` page at `example.com/articles`. You have 10 articles, but you want your template to show only two rows:
37
38 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).
39 1. The second row is titled "Recent Articles" and shows only the 2nd- to 4th-most recently published articles.
40
41 ```go-html-template {file="layouts/section/articles.html"}
42 {{ define "main" }}
43 <section class="row featured-article">
44 <h2>Featured Article</h2>
45 {{ range first 1 .Pages.ByPublishDate.Reverse }}
46 <header>
47 <h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
48 </header>
49 <p>{{ .Description }}</p>
50 {{ end }}
51 </section>
52 <div class="row recent-articles">
53 <h2>Recent Articles</h2>
54 {{ range first 3 (after 1 .Pages.ByPublishDate.Reverse) }}
55 <section class="recent-article">
56 <header>
57 <h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3>
58 </header>
59 <p>{{ .Description }}</p>
60 </section>
61 {{ end }}
62 </div>
63 {{ end }}
64 ```
65
66 [`first`]: /functions/collections/first/
67 [`slice`]: /functions/collections/slice/
68 [powerful sorting methods]: /quick-reference/page-collections/#sort