Skip to content

Instantly share code, notes, and snippets.

@ardentsword
Forked from SimonSimCity/pagination.html.twig
Last active March 12, 2019 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ardentsword/4286d4bc71cb783657f6aa1ab52176fd to your computer and use it in GitHub Desktop.
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.
{#
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