-
-
Save ardentsword/4286d4bc71cb783657f6aa1ab52176fd 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. Modified to work with bootstrap 4.
This file contains hidden or 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: (removed because website is dead, is spam) | |
Updated by: ArdentSword | |
Modified to work with symfony/bootstrap and have default values for some of the arguments | |
Tested with Symfony 3.2/3.3/3.4 and Bootstrap 4 alpha/beta/release | |
Parameters: | |
* currentPage (int) : the current page you are in | |
* lastPage (int) : represents the total number of existing pages | |
* [currentFilters (array) = current route params] : associative array that contains the current route-arguments | |
* [paginationPath (string) = current route] : the route name to use for links | |
* [showAlwaysFirstAndLast (bool) = true] : Always show first and last link (just disabled) | |
#} | |
{% 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 %} | |
{% if currentFilters is not defined %}{% set currentFilters = app.request.attributes.get('_route_params') %}{% endif %} | |
{% if paginationPath is not defined %}{% set paginationPath = app.request.attributes.get('_route') %}{% endif %} | |
{% if showAlwaysFirstAndLast is not defined %}{% set showAlwaysFirstAndLast = true %}{% endif %} | |
<nav aria-label="Page navigation example"> | |
<ul class="pagination"> | |
{% if currentPage > 1 %} | |
<li class="page-item"><a class="page-link" href="{{ path(paginationPath, currentFilters|merge({page: currentPage-1})) }}">Previous</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 %} | |
<span class="sep-dots">...</span> | |
{% 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="#">Previous</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) %} | |
<span class="sep-dots">...</span> | |
{% 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})) }}">Next</a></li> | |
{% elseif showAlwaysFirstAndLast %} | |
<li class="page-item disabled"><a class="page-link" href="#">Next</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