Skip to content

Instantly share code, notes, and snippets.

@lemmingz
Forked from SimonSimCity/pagination.html.twig
Last active September 17, 2017 21:12
Show Gist options
  • Save lemmingz/8942bf1541fd3d6542542e485096d6dd to your computer and use it in GitHub Desktop.
Save lemmingz/8942bf1541fd3d6542542e485096d6dd to your computer and use it in GitHub Desktop.
A gist for pagination in Twig, based on the total number of pages, the current page and some URL-settings.
{#
Source: http://dev.dbl-a.com/symfony-2-0/symfony2-and-twig-pagination/
Updated by: Simon Schick <simonsimcity@gmail.com>
Parameters:
* currentFilters (array) : associative array that contains the current route-arguments
* currentPage (int) : the current page you are in
* paginationPath (string) : the route name to use for links
* showAlwaysFirstAndLast (bool) : Always show first and last link (just disabled)
* lastPage (int) : represents the total number of existing pages
#}
{% spaceless %}
{% if lastPage > 1 %}
{# the number of first and last pages to be displayed #}
{% set extremePagesLimit = 1 %}
{# the number of pages that are displayed around the active page #}
{% set nearbyPagesLimit = 2 %}
{% set currentFilters = currentFilters|default({}) %}
{% set paginationPath = paginationPath |default(app.request.attributes.get('_route'))%}
{% set showAlwaysFirstAndLast = showAlwaysFirstAndLast|default(true) %}
<nav aria-label="Page navigation">
<ul class="pagination">
{% if currentPage > 1 %}
<li class="page-item"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({page: currentPage-1})) }}">«</a></li>
{% for i in range(1, extremePagesLimit) if ( i < currentPage - nearbyPagesLimit ) %}
<li class="page-item"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a></li>
{% endfor %}
{% if extremePagesLimit + 1 < currentPage - nearbyPagesLimit %}
<li class="page-item disabled"><a class="page-link" href="#"><span class="sep-dots">...</span></a></li>
{% endif %}
{% for i in range(currentPage-nearbyPagesLimit, currentPage-1) if ( i > 0 ) %}
<li class="page-item"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a></li>
{% endfor %}
{% elseif showAlwaysFirstAndLast %}
<li class="page-item disabled"><a class="page-link" href="#">«</a></li>
{% endif %}
<li class="page-item active"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({ page: currentPage })) }}">{{ currentPage }}</a></li>
{% if currentPage < lastPage %}
{% for i in range(currentPage+1, currentPage + nearbyPagesLimit) if ( i <= lastPage ) %}
<li class="page-item"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a></li>
{% endfor %}
{% if (lastPage - extremePagesLimit) > (currentPage + nearbyPagesLimit) %}
<li class="page-item disabled"><a class="page-link" href="#"><span class="sep-dots">...</span></a></li>
{% endif %}
{% for i in range(lastPage - extremePagesLimit+1, lastPage) if ( i > currentPage + nearbyPagesLimit ) %}
<li class="page-item"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a></li>
{% endfor %}
<li class="page-item"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({page: currentPage+1})) }}">»</a></li>
{% elseif showAlwaysFirstAndLast %}
<li class="page-item disabled"><a class="page-link" href="#">»</a></li>
{% endif %}
</ul>
</nav>
{% endif %}
{% endspaceless %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment