From f19ea53cc452a247aa6a955b980dfc781ea76c26 Mon Sep 17 00:00:00 2001 From: ParitoshBh Date: Thu, 16 May 2019 20:07:45 -0400 Subject: [PATCH] Add pagination. Default list size is 10 --- grav-coder.yaml | 4 +++- templates/blog.html.twig | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/grav-coder.yaml b/grav-coder.yaml index 21726ea..89cd5ea 100644 --- a/grav-coder.yaml +++ b/grav-coder.yaml @@ -6,4 +6,6 @@ navbar: footer: copyright: Grav Coder post: - navigation: true \ No newline at end of file + navigation: true +pagination: + count: 10 \ No newline at end of file diff --git a/templates/blog.html.twig b/templates/blog.html.twig index 2000fd7..378e562 100644 --- a/templates/blog.html.twig +++ b/templates/blog.html.twig @@ -1,6 +1,5 @@ {% extends 'partials/base.html.twig' %} -{% set collection = page.collection() %} -{% set blog = page.find(header_var('blog_url')|defined(theme_var('blog-page'))) %} + {% if uri.param('category') %} {% set page_title = "Category: " ~ uri.param('category') %} {% else %} @@ -11,17 +10,51 @@ {% block body %} +{# Variables for pagination #} +{% set collection = page.collection({ + 'items': '@self.children', + 'pagination': true, + 'order': {'by': 'date', 'dir': 'desc'}, + 'limit': theme_config.pagination.count}) +%} +{% set itemsInCollection = page.collection({'items': collection.params.items})|length %} +{% set currentPage = uri.param('page')|default('1') %} +{% set itemsPerPage = collection.params.limit %} +{% set pagesInCollection = (itemsInCollection / itemsPerPage)|round(0, 'ceil') %} +
+ + {# Header #} + + {# Listing #}
{% for child in collection %} {% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %} {% endfor %}
+ + {# Pagination #} + {% if itemsInCollection > itemsPerPage %} +
+ {% if currentPage != '1' %} +
+ Newer +
+ {% endif %} + {% if (currentPage + 1) <= pagesInCollection %} +
+ Older + +
+ {% endif %} +
+ {% endif %} +
{% endblock %} \ No newline at end of file