| 1 |
--- |
| 2 |
title: Paginate |
| 3 |
description: Returns a pager created by paginating the given collection of pages. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: page.Pager |
| 9 |
signatures: ['PAGE.Paginate COLLECTION [N]'] |
| 10 |
--- |
| 11 |
|
| 12 |
Pagination is the process of splitting a list page into two or more pagers, where each pager contains a subset of the page collection and navigation links to other pagers. |
| 13 |
|
| 14 |
By default, the number of elements on each pager is determined by your [project configuration][]. The default is `10`. Override that value by providing a second argument, an integer, when calling the `Paginate` method. |
| 15 |
|
| 16 |
> [!NOTE] |
| 17 |
> There is also a `Paginator` method on `Page` objects, but it can neither filter nor sort the page collection. |
| 18 |
> |
| 19 |
> The `Paginate` method is more flexible. |
| 20 |
|
| 21 |
You can invoke pagination in [home][], [section][], [taxonomy][], and [term][] templates. |
| 22 |
|
| 23 |
```go-html-template {file="layouts/section.html"} |
| 24 |
{{ $pages := where .Site.RegularPages "Section" "articles" }} |
| 25 |
{{ $pages = $pages.ByTitle }} |
| 26 |
{{ range (.Paginate $pages 7).Pages }} |
| 27 |
<h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2> |
| 28 |
{{ end }} |
| 29 |
{{ partial "pagination.html" . }} |
| 30 |
``` |
| 31 |
|
| 32 |
In the example above, we: |
| 33 |
|
| 34 |
1. Build a page collection |
| 35 |
1. Sort the collection by title |
| 36 |
1. Paginate the collection, with 7 elements per pager |
| 37 |
1. Range over the paginated page collection, rendering a link to each page |
| 38 |
1. Call the embedded pagination template to create navigation links between pagers |
| 39 |
|
| 40 |
> [!NOTE] |
| 41 |
> Please note that the results of pagination are cached. Once you have invoked either the `Paginator` or `Paginate` method, the paginated collection is immutable. Additional invocations of these methods will have no effect. |
| 42 |
|
| 43 |
[home]: /templates/types/#home |
| 44 |
[project configuration]: /configuration/pagination/ |
| 45 |
[section]: /templates/types/#section |
| 46 |
[taxonomy]: /templates/types/#taxonomy |
| 47 |
[term]: /templates/types/#term |