getgrav / grav-plugin-pagination

Grav Pagination Plugin
https://getgrav.org
MIT License
27 stars 20 forks source link

Query parameters cleared by pagination #49

Open NicoHood opened 4 years ago

NicoHood commented 4 years ago

I am using query parameters www.website.com/page:1?param=true for additional filtering. But the current pagination template removes those queries. It would be nice to integrate this into the plugin.

This is my solution:

{% set pagination = pagination|default(page.collection.params.pagination) %}
{% set base_url = base_url|default(page.url) %}

{% if pagination|length > 1 %}

<ul class="pagination">
    {% if pagination.hasPrev %}
        {% set url =  (base_url ~ pagination.params ~ pagination.prevUrl ~ '?' ~ uri.query())|replace({'//':'/'}) %}
        <li><a rel="prev" href="{{ url }}">&laquo;</a></li>
    {% else %}
        <li><span>&laquo;</span></li>
    {% endif %}

    {% for paginate in pagination %}

        {% if paginate.isCurrent %}
            <li><span class="active">{{ paginate.number }}</span></li>
        {% elseif paginate.isInDelta %}
            {% set url = (base_url ~ pagination.params ~ paginate.url ~ '?' ~ uri.query())|replace({'//':'/'}) %}
            <li><a href="{{ url }}">{{ paginate.number }}</a></li>
        {% elseif paginate.isDeltaBorder %}
            <li class="gap"><span>&hellip;</span></li>
        {% endif %}

    {% endfor %}
    {% if pagination.hasNext %}
        {% set url = (base_url ~ pagination.params ~ pagination.nextUrl ~ '?' ~ uri.query())|replace({'//':'/'}) %}
        <li><a rel="next" href="{{ url }}">&raquo;</a></li>
    {% else %}
        <li><span>&raquo;</span></li>
    {% endif %}
</ul>

{% endif %}

Maybe it would make sense to add a variable to enable this new feature to not break older themes. Feel free to use that code!

mahagr commented 4 years ago

Just a note:

There are new pagination classes which already support this: https://github.com/getgrav/grav/tree/develop/system/src/Grav/Framework/Pagination

NicoHood commented 4 years ago

Wouldnt it make sense to integrate such into the quark theme?