paginate
Splits an array's items across multiple pages.
Because for
loops are limited to 50 iterations per page, you need to use the paginate
tag to
iterate over an array that has more than 50 items. The following arrays can be paginated:
article.comments
blog.articles
collections
collection.products
customer.addresses
customer.orders
pages
product.variants
search.results
settings
settings
Within the paginate
tag, you have access to the paginate
object. You can use this
object, or the filter, to build page navigation.
Syntax
The array to be looped over.
The number of array items to include per page, between 1 and 250.
An item in the array being looped.
Content for each loop iteration.
{% paginate collection.products by 5 %}
{% for product in collection.products -%}
{{ product.title }}
{%- endfor %}
{{- paginate | default_pagination }}
{% endpaginate %}
{
"collection": {
"products": [
{
"title": "Blue Mountain Flower"
},
{
"title": "Charcoal"
},
{
"title": "Crocodile tears"
},
{
"title": "Dandelion milk"
},
{
"title": "Draught of Immortality"
}
],
"products_count": 19
}
}
Output
Paginating setting arrays
To allow the pagination of and
settings to operate independently from other paginated lists on a page, these lists use a pagination query parameter with a unique key. The key is automatically assigned by the
paginate
tag, and you don't need to reference the key in your code. However, you can access the key using .
To paginate two arrays independently without refreshing the entire page, you can use the Section Rendering API.
Limit data fetching
The limit
parameter of the for
tag controls the number of iterations, but not the amount of information fetched. Using the paginate
tag with a matching can reduce the data queried, leading to faster server response times.
For example, referencing collection.products
will fetch up to 50 products by default, regardless of the forloop's limit
parameter. Use paginate
and set a to limit the amount of data fetched, and opt not to display any pagination controls.
More data than requested in a specific section may be returned. Because of this, make sure to include both paginate
and limit
when using this technique.
{% paginate collection.products by 4 %}
{% for product in collection.products limit: 4 -%}
{{ product.title }}
{%- endfor %}
{% endpaginate -%}
<!-- Less performant method -->
{% for product in collection.products limit: 4 -%}
{{ product.title }}
{%- endfor -%}
{
"collection": {
"products": [
{
"title": "Blue Mountain Flower"
},
{
"title": "Charcoal"
},
{
"title": "Crocodile tears"
},
{
"title": "Dandelion milk"
}
],
"products_count": 19
}
}
Output
paginate tag parameters
window_size
Syntax
Set the window size of the pagination. The window size is the number of pages that should be visible in the pagination navigation.
{% paginate collection.products by 3, window_size: 1 %}
{% for product in collection.products -%}
{{ product.title }}
{%- endfor %}
{{- paginate | default_pagination }}
{% endpaginate %}
{
"collection": {
"products": [
{
"title": "Blue Mountain Flower"
},
{
"title": "Charcoal"
},
{
"title": "Crocodile tears"
}
],
"products_count": 19
}
}