Skip to content

Instantly share code, notes, and snippets.

@barchard
Forked from SimonSimCity/pagination.html.twig
Last active February 20, 2016 13:10
Show Gist options
  • Save barchard/9942534 to your computer and use it in GitHub Desktop.
Save barchard/9942534 to your computer and use it in GitHub Desktop.
Updated for Bootstrap v3. Fixing issue with Next/Previous not using arrows. Fixing issue where in (in previous Bootstrap comment) arrows were not disabled if "showAlways" was true.
{#
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 %}
<div class="row">
<ul class="pagination">
{% if currentPage > 1 %}
<li><a href="{{ paginationPath }}{{ currentPage-1 }}">&laquo;</a></li>
{% for i in range(1, extremePagesLimit) if ( i < currentPage - nearbyPagesLimit ) %}
<li><a href="{{ paginationPath }}{{ i }}">{{ i }}</a></li>
{% endfor %}
{% if extremePagesLimit + 1 < currentPage - nearbyPagesLimit %}
<li class="disabled"><a>...</a></li>
{% endif %}
{% for i in range(currentPage-nearbyPagesLimit, currentPage-1) if ( i > 0 ) %}
<li><a href="{{ paginationPath }}{{ i }}">{{ i }}</a></li>
{% endfor %}
{% elseif showAlwaysFirstAndLast %}
<li class="disabled"><a>&laquo;</a></li>
{% endif %}
<li class="active"><a>{{ currentPage }}</a></li>
{% if currentPage < lastPage %}
{% for i in range(currentPage+1, currentPage + nearbyPagesLimit) if ( i <= lastPage ) %}
<li><a href="{{ paginationPath }}{{ i }}">{{ i }}</a></li>
{% endfor %}
{% if (lastPage - extremePagesLimit) > (currentPage + nearbyPagesLimit) %}
<li class="disabled"><a>...</a></li>
{% endif %}
{% for i in range(lastPage - extremePagesLimit+1, lastPage) if ( i > currentPage + nearbyPagesLimit ) %}
<li><a href="{{ paginationPath }}{{ i }}">{{ i }}</a></li>
{% endfor %}
<li><a href="{{ paginationPath }}{{ currentPage+1 }}">&raquo;</a></li>
{% elseif showAlwaysFirstAndLast %}
<li class="disabled"><a>&raquo;</a></li>
{% endif %}
</ul>
</div>
{% endif %}
{% endspaceless %}
@dommermuth
Copy link

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment