-
-
Save omerucel/219ee06444c58df097f805da3f9a7b65 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
Source: http://dev.dbl-a.com/symfony-2-0/symfony2-and-twig-pagination/ | |
Updated by: Simon Schick <[email protected]> | |
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 = 3 %} | |
{# the number of pages that are displayed around the active page #} | |
{% set nearbyPagesLimit = 2 %} | |
<ul class="pagination"> | |
{% if currentPage > 1 %} | |
<li class="page-item"> | |
<a href="{{ paginationPath ~ '?' ~ currentFilters|merge({page: currentPage-1})|url_encode }}" class="page-link">Previous</a> | |
</li> | |
{% for i in range(1, extremePagesLimit) if ( i < currentPage - nearbyPagesLimit ) %} | |
<li class="page-item"> | |
<a href="{{ paginationPath ~ '?' ~ currentFilters|merge({page: i})|url_encode }}" class="page-link">{{ i }}</a> | |
</li> | |
{% endfor %} | |
{% if extremePagesLimit + 1 < currentPage - nearbyPagesLimit %} | |
<li class="page-item disabled"><a href="#" class="page-link">...</a></li> | |
{% endif %} | |
{% for i in range(currentPage-nearbyPagesLimit, currentPage-1) if ( i > 0 ) %} | |
<li class="page-item"> | |
<a href="{{ paginationPath ~ '?' ~ currentFilters|merge({page: i})|url_encode }}" class="page-link">{{ i }}</a> | |
</li> | |
{% endfor %} | |
{% elseif showAlwaysFirstAndLast %} | |
<li class="page-item disabled"><a href="#" class="page-link">Previous</a></li> | |
{% endif %} | |
<li class="page-item"> | |
<a href="{{ paginationPath ~ '?' ~ currentFilters|merge({ page: currentPage })|url_encode }}" | |
class="page-link active">{{ currentPage }}</a> | |
</li> | |
{% if currentPage < lastPage %} | |
{% for i in range(currentPage+1, currentPage + nearbyPagesLimit) if ( i <= lastPage ) %} | |
<li class="page-item"> | |
<a href="{{ paginationPath ~ '?' ~ currentFilters|merge({page: i})|url_encode }}" class="page-link">{{ i }}</a> | |
</li> | |
{% endfor %} | |
{% if (lastPage - extremePagesLimit) > (currentPage + nearbyPagesLimit) %} | |
<li class="page-item disabled"><a href="#" class="page-link">...</a></li> | |
{% endif %} | |
{% for i in range(lastPage - extremePagesLimit+1, lastPage) if ( i > currentPage + nearbyPagesLimit ) %} | |
<li class="page-item"> | |
<a href="{{ paginationPath ~ '?' ~ currentFilters|merge({page: i})|url_encode }}" class="page-link">{{ i }}</a> | |
</li> | |
{% endfor %} | |
<li class="page-item"> | |
<a href="{{ paginationPath ~ '?' ~ currentFilters|merge({page: currentPage+1})|url_encode }}" class="page-link">Next</a> | |
</li> | |
{% elseif showAlwaysFirstAndLast %} | |
<li class="page-item disabled"><a href="#" class="page-link">Next</a></li> | |
{% endif %} | |
</ul> | |
{% endif %} | |
{% endspaceless %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
without path function. added bootstrap v4 support.