You can use this twig code on any template where you would like to add a listing with pagination.
To configure your page collection for your needs look at the Grav documentation regarding collections.
This is where you define your collection
TWIG
{% set collection = page.collection({
'items': '@self.children',
'pagination': true,
'order': {'by': 'title'},
'limit': 2})
%}
These are the needed variables/calculations that the pagination needs.
Usually you won't need to change these
TWIG
{% set itemsInCollection = page.collection({'items': collection.params.items})|length %}
{% set itemsPerPage = collection.params.limit %}
{% set pagesInCollection = (itemsInCollection / itemsPerPage)|round(0, 'ceil') %}
{% set currentPage = uri.param('page')|default('1') %}
This is where the pagination gets displayed. Change with caution
TWIG
<ul class="pagination">
{% for i in range(1, pagesInCollection) %}
<li {% if currentPage is same as('' ~ i ~ '') %} class="currentpage" {% endif %}>
<a href="{{ page.url ~ '/page' ~ system.param_sep ~ i }}">{{ i }}</a>
</li>
{% endfor %}
</ul>
last edited 03/12/18 by Kenneth Joris