Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Showcase

Pagination - simple & small twig version - based on page collections

Started by Kenneth Joris 8 years ago · 1 replies · 3631 views
8 years ago

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
8 years ago

Another version: Listing of pages that use a template.

`<h2>Amount of listing items - pagination by template</h2>
<p><strong>When using dynamic twig variables, turn off the page cache !</strong></p>

TWIG
    <!-- setup variables -->
    {% set headerCollection = page.collection({ 'items': '@root.children' }) %}
    {% set typesInCollection = headerCollection.ofOneOfTheseTypes(['image-demo','divider-demo','fields-demo']) %}
    {% set itemsPerPage = 2 %}

    <!-- page amount calculations -->
    {% set itemsInCollection = typesInCollection|length %}
    {% set pagesInCollection = (itemsInCollection / itemsPerPage)|round(0, 'ceil') %}

    <!-- listing item amount calculations -->
    {% set currentPage = uri.param('page')|default('1') %}
    {% set sliceOffset = (currentPage * itemsPerPage) - itemsPerPage %}

    <!-- fallback - if itemsPerPage is set to 1 -->
    {% if not itemsPerPage is same as(1) %}
        {% set sliceLength =  sliceOffset + itemsPerPage %}
    {% else %}
        {% if sliceOffset ==  0 %}
            {% set sliceLength =  1 %}
        {% else %}
            {% set sliceLength =  sliceOffset %}
        {% endif %}
    {% endif %}

    <!-- the rendering -->
    {% set theCollection = typesInCollection.slice(sliceOffset,sliceLength) %}

    <!-- the pagination -->
    <ul class="pagination">
    {% for number in range(1, pagesInCollection) %}
        <li {% if currentPage is same as('' ~ number ~ '') %} class="currentpage" {% endif %}>
            <a href="{{ page.url ~ '/page' ~ system.param_sep ~ number }}">{{ number }}</a>
        </li>
    {% endfor %}
    </ul>

    <!-- the list items -->
    {% for child in theCollection %} 
       {{ child.title }} <br>
    {% endfor %}`
👍 1

Suggested topics

Topic Participants Replies Views Activity
Showcase · by Julien Perret, 3 months ago
3 136 1 month ago
Showcase · by Julien Perret, 2 months ago
4 145 2 months ago
Showcase · by Stuart, 5 months ago
1 126 5 months ago
Showcase · by Gez, 8 months ago
4 258 8 months ago
Showcase · by Roger Parkinson, 10 months ago
0 111 10 months ago