| 1 |
--- |
| 2 |
title: Pagination |
| 3 |
description: Split a list page into two or more subsets. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
weight: 160 |
| 7 |
aliases: [/extras/pagination,/doc/pagination/] |
| 8 |
--- |
| 9 |
|
| 10 |
Displaying a large page collection on a list page is not user-friendly: |
| 11 |
|
| 12 |
- A massive list can be intimidating and difficult to navigate. Users may get lost in the sheer volume of information. |
| 13 |
- Large pages take longer to load, which can frustrate users and lead to them abandoning the site. |
| 14 |
- Without any filtering or organization, finding a specific item becomes a tedious scrolling exercise. |
| 15 |
|
| 16 |
Improve usability by paginating `home`, `section`, `taxonomy`, and `term` pages. |
| 17 |
|
| 18 |
> [!NOTE] |
| 19 |
> The most common templating mistake related to pagination is invoking pagination more than once for a given list page. See the [caching](#caching) section below. |
| 20 |
|
| 21 |
## Terminology |
| 22 |
|
| 23 |
paginate |
| 24 |
: To split a [list page](g) into two or more subsets. |
| 25 |
|
| 26 |
pagination |
| 27 |
: The process of paginating a list page. |
| 28 |
|
| 29 |
pager |
| 30 |
: Created during pagination, a pager contains a subset of a list page and navigation links to other pagers. |
| 31 |
|
| 32 |
paginator |
| 33 |
: A collection of pagers. |
| 34 |
|
| 35 |
## Configuration |
| 36 |
|
| 37 |
See [configure pagination][]. |
| 38 |
|
| 39 |
## Methods |
| 40 |
|
| 41 |
To paginate a `home`, `section`, `taxonomy`, or `term` page, invoke either of these methods on the `Page` object in the corresponding template: |
| 42 |
|
| 43 |
- [`Paginate`][] |
| 44 |
- [`Paginator`][] |
| 45 |
|
| 46 |
The `Paginate` method is more flexible, allowing you to: |
| 47 |
|
| 48 |
- Paginate any page collection |
| 49 |
- Filter, sort, and group the page collection |
| 50 |
- Override the number of pages per pager as defined in your project configuration |
| 51 |
|
| 52 |
By comparison, the `Paginator` method paginates the page collection passed into the template, and you cannot override the number of pages per pager. |
| 53 |
|
| 54 |
## Examples |
| 55 |
|
| 56 |
To paginate a list page using the `Paginate` method: |
| 57 |
|
| 58 |
```go-html-template |
| 59 |
{{ $pages := where site.RegularPages "Type" "posts" }} |
| 60 |
{{ $paginator := .Paginate $pages.ByTitle 7 }} |
| 61 |
|
| 62 |
{{ range $paginator.Pages }} |
| 63 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 64 |
{{ end }} |
| 65 |
|
| 66 |
{{ partial "pagination.html" . }} |
| 67 |
``` |
| 68 |
|
| 69 |
In the example above, we: |
| 70 |
|
| 71 |
1. Build a page collection |
| 72 |
1. Sort the page collection by title |
| 73 |
1. Paginate the page collection, with 7 pages per pager |
| 74 |
1. Range over the paginated page collection, rendering a link to each page |
| 75 |
1. Call the embedded pagination template to create navigation links between pagers |
| 76 |
|
| 77 |
To paginate a list page using the `Paginator` method: |
| 78 |
|
| 79 |
```go-html-template |
| 80 |
{{ range .Paginator.Pages }} |
| 81 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 82 |
{{ end }} |
| 83 |
|
| 84 |
{{ partial "pagination.html" . }} |
| 85 |
``` |
| 86 |
|
| 87 |
In the example above, we: |
| 88 |
|
| 89 |
1. Paginate the page collection passed into the template, with the default number of pages per pager |
| 90 |
1. Range over the paginated page collection, rendering a link to each page |
| 91 |
1. Call the embedded pagination template to create navigation links between pagers |
| 92 |
|
| 93 |
## Caching |
| 94 |
|
| 95 |
> [!NOTE] |
| 96 |
> The most common templating mistake related to pagination is invoking pagination more than once for a given list page. |
| 97 |
|
| 98 |
Regardless of pagination method, the initial invocation is cached and cannot be changed. If you invoke pagination more than once for a given list page, subsequent invocations use the cached result. This means that subsequent invocations will not behave as written. |
| 99 |
|
| 100 |
When paginating conditionally, do not use the `compare.Conditional` function due to its eager evaluation of arguments. Use an `if-else` construct instead. |
| 101 |
|
| 102 |
## Grouping |
| 103 |
|
| 104 |
Use pagination with any of the [grouping methods][]. For example: |
| 105 |
|
| 106 |
```go-html-template |
| 107 |
{{ $pages := where site.RegularPages "Type" "posts" }} |
| 108 |
{{ $paginator := .Paginate ($pages.GroupByDate "Jan 2006") }} |
| 109 |
|
| 110 |
{{ range $paginator.PageGroups }} |
| 111 |
<h2>{{ .Key }}</h2> |
| 112 |
{{ range .Pages }} |
| 113 |
<h3><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h3> |
| 114 |
{{ end }} |
| 115 |
{{ end }} |
| 116 |
|
| 117 |
{{ partial "pagination.html" . }} |
| 118 |
``` |
| 119 |
|
| 120 |
## Navigation |
| 121 |
|
| 122 |
As shown in the examples above, the easiest way to add navigation between pagers is with Hugo's embedded pagination template: |
| 123 |
|
| 124 |
```go-html-template |
| 125 |
{{ partial "pagination.html" . }} |
| 126 |
``` |
| 127 |
|
| 128 |
The embedded pagination template has two formats: `default` and `terse`. The above is equivalent to: |
| 129 |
|
| 130 |
```go-html-template |
| 131 |
{{ partial "pagination.html" (dict "page" . "format" "default") }} |
| 132 |
``` |
| 133 |
|
| 134 |
The `terse` format has fewer controls and page slots, consuming less space when styled as a horizontal list. To use the `terse` format: |
| 135 |
|
| 136 |
```go-html-template |
| 137 |
{{ partial "pagination.html" (dict "page" . "format" "terse") }} |
| 138 |
``` |
| 139 |
|
| 140 |
> [!NOTE] |
| 141 |
> To override Hugo's embedded pagination template, copy the [source code][] to a file with the same name in the `layouts/_partials` directory, then call it from your templates using the [`partial`][] function: |
| 142 |
> |
| 143 |
> `{{ partial "pagination.html" . }}` |
| 144 |
|
| 145 |
Create custom navigation components using any of the `Pager` methods: |
| 146 |
|
| 147 |
{{% render-list-of-pages-in-section path=/methods/pager %}} |
| 148 |
|
| 149 |
## Structure |
| 150 |
|
| 151 |
The example below depicts the published site structure when paginating a list page. |
| 152 |
|
| 153 |
With this content: |
| 154 |
|
| 155 |
```tree |
| 156 |
content/ |
| 157 |
├── posts/ |
| 158 |
│ ├── _index.md |
| 159 |
│ ├── post-1.md |
| 160 |
│ ├── post-2.md |
| 161 |
│ ├── post-3.md |
| 162 |
│ └── post-4.md |
| 163 |
└── _index.md |
| 164 |
``` |
| 165 |
|
| 166 |
And this project configuration: |
| 167 |
|
| 168 |
{{< code-toggle file=hugo >}} |
| 169 |
[pagination] |
| 170 |
disableAliases = false |
| 171 |
pagerSize = 2 |
| 172 |
path = 'page' |
| 173 |
{{< /code-toggle >}} |
| 174 |
|
| 175 |
And this _section_ template: |
| 176 |
|
| 177 |
```go-html-template {file="layouts/section.html"} |
| 178 |
{{ range (.Paginate .Pages).Pages }} |
| 179 |
<h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> |
| 180 |
{{ end }} |
| 181 |
|
| 182 |
{{ partial "pagination.html" . }} |
| 183 |
``` |
| 184 |
|
| 185 |
The published site has this structure: |
| 186 |
|
| 187 |
```tree |
| 188 |
public/ |
| 189 |
├── posts/ |
| 190 |
│ ├── page/ |
| 191 |
│ │ ├── 1/ |
| 192 |
│ │ │ └── index.html <-- alias to public/posts/index.html |
| 193 |
│ │ └── 2/ |
| 194 |
│ │ └── index.html |
| 195 |
│ ├── post-1/ |
| 196 |
│ │ └── index.html |
| 197 |
│ ├── post-2/ |
| 198 |
│ │ └── index.html |
| 199 |
│ ├── post-3/ |
| 200 |
│ │ └── index.html |
| 201 |
│ ├── post-4/ |
| 202 |
│ │ └── index.html |
| 203 |
│ └── index.html |
| 204 |
└── index.html |
| 205 |
``` |
| 206 |
|
| 207 |
To disable alias generation for the first pager, change your project configuration: |
| 208 |
|
| 209 |
{{< code-toggle file=hugo >}} |
| 210 |
[pagination] |
| 211 |
disableAliases = true |
| 212 |
pagerSize = 2 |
| 213 |
path = 'page' |
| 214 |
{{< /code-toggle >}} |
| 215 |
|
| 216 |
Now the published site will have this structure: |
| 217 |
|
| 218 |
```tree |
| 219 |
public/ |
| 220 |
├── posts/ |
| 221 |
│ ├── page/ |
| 222 |
│ │ └── 2/ |
| 223 |
│ │ └── index.html |
| 224 |
│ ├── post-1/ |
| 225 |
│ │ └── index.html |
| 226 |
│ ├── post-2/ |
| 227 |
│ │ └── index.html |
| 228 |
│ ├── post-3/ |
| 229 |
│ │ └── index.html |
| 230 |
│ ├── post-4/ |
| 231 |
│ │ └── index.html |
| 232 |
│ └── index.html |
| 233 |
└── index.html |
| 234 |
``` |
| 235 |
|
| 236 |
[`Paginate`]: /methods/page/paginate/ |
| 237 |
[`Paginator`]: /methods/page/paginator/ |
| 238 |
[`partial`]: /functions/partials/include/ |
| 239 |
[configure pagination]: /configuration/pagination/ |
| 240 |
[grouping methods]: /quick-reference/page-collections/#group |
| 241 |
[source code]: <{{% eturl pagination %}}> |