Closed bricebou closed 6 years ago
I've tried to work on a better way to integrate this pagination on different template files and I produced this how-to on my website: http://momh.fr/tutos/Pico/pico_twig_pagination
@PhrozenByte You can copy this, if you think it's good and useful, into the picocms website. If you find a better way, let me know !
Here it is: inside your index.twig
(for example), paste this code and set the variables:
{# number of pages to display per page #}
{% set pagi_slice_length = 5 %}
{# the array of pages to use #}
{% set pagi_pages_array = pages %}
{# the base URL of the page the pagination applied
it can be a more complex string, concatenated like this:
base_url ~ "/tag/" ~ current_tag
#}
{% set pagi_base_url = base_url %}
{# the name to use as the URL parameter (?page=xxx) #}
{% set pagi_http_param = 'page' %}
{% if url_param(pagi_http_param, 'int') is not null %}
{% set pagi_basepage = url_param(pagi_http_param, 'int') %}
{% set pagi_slice_start = pagi_slice_length * (pagi_basepage - 1) %}
{% else %}
{% set pagi_slice_start = 0 %}
{% set pagi_basepage = 1 %}
{% endif %}
{% set pagi_maxpage = (pagi_pages_array|length / pagi_slice_length)|round(0, 'ceil') %}
{% for page in pagi_pages_array|slice(pagi_slice_start, pagi_slice_length, preserve_keys) %}
<article>
<h2 class="h1"><a href="{{ page.url }}">{{ page.title }}</a></h2>
</article>
{% endfor %}
{% include 'includes/pagination.twig' with [pagi_basepage, pagi_maxpage, pagi_base_url, pagi_http_param] %}
You have to create a includes/pagination.twig
file in which you have to paste this code:
{% set pagi_next_page = pagi_basepage + 1 %}
{% set pagi_prev_page = pagi_basepage - 1 %}
{% if pagi_maxpage > 1 %}
<ul>
{% if pagi_prev_page is not null %}
<li class="pi" {% if pagi_prev_page < 1 %}style="visibility: hidden;"{% endif %}><a href="{% if pagi_prev_page == 1 %}{{ pagi_base_url }}{% else %}{{ pagi_base_url ~ '/?' ~ pagi_http_param ~ '=' ~ pagi_prev_page }}{% endif %}" title="Newest posts" id="prev_page_link">Newest posts</a></li>
{% endif %}
{% for item in range(1, pagi_maxpage|number_format) %}
<li class="pi {%if item == pagi_basepage %} pi_a{%endif %}"><a href="{% if item == 1 %}{{ pagi_base_url }}{% else %}{{ pagi_base_url ~ '/?' ~ pagi_http_param ~ '=' ~ item }}{% endif %}" class="page_item {%if item == pagi_basepage %} page_active{%endif %}">{{ item }}</a></li>
{% endfor %}
{% if pagi_next_page is not null %}
<li class="pi" {% if pagi_next_page > pagi_maxpage %}style="visibility: hidden;"{% endif %}><a href="{{ pagi_base_url ~ '/?' ~ pagi_http_param ~ '=' ~ pagi_next_page }}" title="Older posts" id="next_page_link">Older posts</a></li>
{% endif %}
</ul>
{% endif %}
It's awful, but it works ! ^^
A live example: momh.fr/
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1:
Comment for un-stale-ing this issue
Didn't forget about answering this, just haven't had time for it yet...
No worry @PhrozenByte :-)
I've just updated my second comment (I forgot the pagi_pages_array
variable in my for
loop).
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1:
Comment for un-stale-ing this issue
I'm going to work on adding options to display only X items when the maxpage is too important.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1:
Just in time !
So I've worked on a a way to reduce the number of displayed pages !
Here is my new includes/pagination.twig
:
{% set pagi_next_page = pagi_basepage + 1 %}
{% set pagi_prev_page = pagi_basepage - 1 %}
{% if pagi_max_near_by is null %}
{% set pagi_max_near_by = 2 %}
{% endif %}
{% if pagi_maxpage > 1 %}
<ul>
{% if pagi_prev_page is not null %}
<li class="pi" {% if pagi_prev_page < 1 %}style="visibility: hidden;"{% endif %}><a href="{% if pagi_prev_page == 1 %}{{ pagi_base_url }}{% else %}{{ pagi_base_url ~ '/?' ~ pagi_http_param ~ '=' ~ pagi_prev_page }}{% endif %}" title="Newest posts" id="prev_page_link">Newest posts</a></li>
{% endif %}
{% if pagi_basepage - pagi_max_near_by > 1 %}
<li class="pi">…</li>
{% endif %}
{% for item in range(1, pagi_maxpage|number_format) %}
<li {% if pagi_basepage - pagi_max_near_by > item or pagi_basepage + pagi_max_near_by < item %}style="display:none;"{% endif %} class="pi {%if item == pagi_basepage %} pi_a{%endif %}"><a href="{% if item == 1 %}{{ pagi_base_url }}{% else %}{{ pagi_base_url ~ '/?' ~ pagi_http_param ~ '=' ~ item }}{% endif %}" class="page_item {%if item == pagi_basepage %} page_active{%endif %}">{{ item }}</a></li>
{% endfor %}
{% if pagi_basepage + pagi_max_near_by < pagi_maxpage %}
<li class="pi">…</li>
{% endif %}
{% if pagi_next_page is not null %}
<li class="pi" {% if pagi_next_page > pagi_maxpage %}style="visibility: hidden;"{% endif %}><a href="{{ pagi_base_url ~ '/?' ~ pagi_http_param ~ '=' ~ pagi_next_page }}" title="Older posts" id="next_page_link">Older posts</a></li>
{% endif %}
</ul>
{% endif %}
I've made a new blog post about that here: http://momh.fr/tutos/Pico/pico_twig_pagination_bis
And you can see it live here: http://momh.fr/?page=5
We already promote plugins and themes on our website, unfortunately there isn't really a counterpart for Twig snippets yet. Anyway, creating one might be a good idea. There's just the currently not really used Cookbook on our website. As always, help is highly appreciated!
The snippets look really great and are exactly the way it should be used. Just some minor notes:
includes/pagination.twig
: {% if pagi_prev_page is not null %}
is obsolete, it never evaluates to false
. I guess you rather want to use {% if pagi_prev_page < 1 %}
(and remove the style="visibility: hidden;"
).includes/pagination.twig
: Same for {% if pagi_next_page is not null %}
(shoulud be {% if pagi_next_page > pagi_maxpage %}
)includes/pagination.twig
: What is pagi_maxpage|number_format
for? The number_format
Twig filter doesn't make much sense here.includes/pagination.twig
: Not really about the Twig snippet, but rather the CSS of your theme: It should be sufficient to mark the active page in the pages list just once: .pi_a
for the li
element and .page_active
for the a
element is redundant, .pi_a
for li
should be sufficient.This issue has been automatically marked as stale because it has not had recent activity. It will be closed in two days if no further activity occurs. Thank you for your contributions! :+1:
HI,
I've worked on a pagination system using Twig and Twig only (after reading https://github.com/picocms/Pico/issues/404 and https://github.com/picocms/Pico/issues/419), that could be used in any pages of my website. I've managed to reproduce the one I generated with my version on Pico-Pagination for my online website, momh.fr.
So here is the code:
I'm quite satisfied (because it's working ^^ and because I can apply this code, with minor changes, to my tag pages, generated with the PicoTags plugin) but unsatisfied due to the double array parsing (the first one to remove some pages - categories - and the second to display the results).
Maybe you can think of a better way of achieving this ?
I've thought of the new Page Tree system. First, I can't figure out how to use it, even with minimal examples I found in https://github.com/picocms/Pico/issues/412 or https://github.com/picocms/Pico/issues/414. Even, after reading https://phrozenbyte.github.io/Pico/in-depth/features/page-tree/
Here is a screenshot of my website content folder.
In the
index.twig
, called only for the content rootindex.md
, I've tried several snippets without success... :-/I think I understand that Page Tree could generate from scratch an array without the simple
index.md
nodes ? So, the length of this array wouldn't have to be modified... Is that correct ? If so, could you give me first steps ?Thanks again and in advance :)