Skip to content

Instantly share code, notes, and snippets.

@goreilly
Last active March 23, 2016 15:21
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 goreilly/11a3a8b6cf99b8a52537 to your computer and use it in GitHub Desktop.
Save goreilly/11a3a8b6cf99b8a52537 to your computer and use it in GitHub Desktop.
Use Knp Sliding Pagination from any template
{# Replicate \Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination::getPaginationData #}
{% set range = range ?? 5 %}
{% if pageCount < current %}
{% set current = pageCount %}
{% endif %}
{% if range > pageCount %}
{% set range = pageCount %}
{% endif %}
{% set delta = ((range / 2) | round(0, 'ceil')) %}
{% if current - delta > pageCount - range %}
{% set pages = range(pageCount - range + 1, pageCount) %}
{% else %}
{% if current - delta < 0 %}
{% set delta = current %}
{% endif %}
{% set offset = current - delta %}
{% set pages = range(offset + 1, offset + range) %}
{% endif %}
{% set proximity = ((range / 2) | round(0, 'floor')) %}
{% set startPage = current - proximity %}
{% set endPage = current + proximity %}
{% if startPage < 1 %}
{% set endPage = min(endPage + (1 - startPage), pageCount) %}
{% endif %}
{% if endPage > pageCount %}
{% set startPage = max(startPage - (endPage - pageCount), 1) %}
{% endif %}
{% set last = pageCount %}
{% if current - 1 > 0 %}
{% set previous = current - 1 %}
{% endif %}
{% if current + 1 <= pageCount %}
{% set next = current + 1 %}
{% endif %}
{% include 'KnpPaginatorBundle:Pagination:twitter_bootstrap_v3_pagination.html.twig' with {
pagesInRange: pages,
route: route ?? app.request.attributes.get('_route'),
query: query ?? app.request.query,
pageParameterName: pageParameterName ?? 'page',
} %}
{% include 'AppBundle::pagination.html.twig' with {pageCount: pages, current: page} %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment