Skip to content

Commit

Permalink
Add pagination. Default list size is 10
Browse files Browse the repository at this point in the history
  • Loading branch information
ParitoshBh committed May 17, 2019
1 parent 7e70724 commit f19ea53
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 3 additions & 1 deletion grav-coder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ navbar:
footer:
copyright: Grav Coder
post:
navigation: true
navigation: true
pagination:
count: 10
37 changes: 35 additions & 2 deletions templates/blog.html.twig
Original file line number Diff line number Diff line change
@@ -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 %}
Expand All @@ -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') %}

<section class="container list">

{# Header #}
<div class="page-header">
<div class="page-title">
<h1>{{ page_title }}</h1>
</div>
</div>

{# Listing #}
<div class="page-content">
{% for child in collection %}
{% include 'partials/blog-list-item.html.twig' with {blog: page, page: child} %}
{% endfor %}
</div>

{# Pagination #}
{% if itemsInCollection > itemsPerPage %}
<div>
{% if currentPage != '1' %}
<div class="float-left">
<i class="far fa-hand-point-left"></i> <a href="{{ page.url ~ '/page' ~ system.param_sep ~ (currentPage - 1) }}">Newer</a>
</div>
{% endif %}
{% if (currentPage + 1) <= pagesInCollection %}
<div class="float-right">
<a href="{{ page.url ~ '/page' ~ system.param_sep ~ (currentPage + 1) }}">Older</a>
<i class="far fa-hand-point-right"></i>
</div>
{% endif %}
</div>
{% endif %}

</section>

{% endblock %}

0 comments on commit f19ea53

Please sign in to comment.