| 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 |
## Basic usage |
| 15 |
|
| 16 |
The following shows `after` being used in conjunction with the [`slice`][] function: |
| 17 |
|
| 18 |
```go-html-template |
| 19 |
{{ $data := slice "one" "two" "three" "four" }} |
| 20 |
<ul> |
| 21 |
{{ range after 2 $data }} |
| 22 |
<li>{{ . }}</li> |
| 23 |
{{ end }} |
| 24 |
</ul> |
| 25 |
``` |
| 26 |
|
| 27 |
The template above is rendered to: |
| 28 |
|
| 29 |
```html |
| 30 |
<ul> |
| 31 |
<li>three</li> |
| 32 |
<li>four</li> |
| 33 |
</ul> |
| 34 |
``` |
| 35 |
|
| 36 |
## Combine with first |
| 37 |
|
| 38 |
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: |
| 39 |
|
| 40 |
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). |
| 41 |
1. The second row is titled "Recent Articles" and shows only the 2nd- to 4th-most recently published articles. |
| 42 |
|
| 43 |
```go-html-template {file="layouts/section/articles.html"} |
| 44 |
{{ define "main" }} |
| 45 |
<section class="row featured-article"> |
| 46 |
<h2>Featured Article</h2> |
| 47 |
{{ range first 1 .Pages.ByPublishDate.Reverse }} |
| 48 |
<header> |
| 49 |
<h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3> |
| 50 |
</header> |
| 51 |
<p>{{ .Description }}</p> |
| 52 |
{{ end }} |
| 53 |
</section> |
| 54 |
<div class="row recent-articles"> |
| 55 |
<h2>Recent Articles</h2> |
| 56 |
{{ range first 3 (after 1 .Pages.ByPublishDate.Reverse) }} |
| 57 |
<section class="recent-article"> |
| 58 |
<header> |
| 59 |
<h3><a href="{{ .RelPermalink }}">{{ .Title }}</a></h3> |
| 60 |
</header> |
| 61 |
<p>{{ .Description }}</p> |
| 62 |
</section> |
| 63 |
{{ end }} |
| 64 |
</div> |
| 65 |
{{ end }} |
| 66 |
``` |
| 67 |
|
| 68 |
[`first`]: /functions/collections/first/ |
| 69 |
[`slice`]: /functions/collections/slice/ |
| 70 |
[powerful sorting methods]: /quick-reference/page-collections/#sort |