Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Twig macro to display a pagination, aka page browser.
{*
Twig macro to display a pagination, aka page browser.
Usage :
First you have to import the macro using "import" :
{% from 'AcmeBundle:Default:Macro/pagebrowser.html.twig' import pagebrowser %}
Then, call the function :
{{ pagebrowser(route, routeParams, totalPages, currentPage, browserSize) }}
Where :
route : the list route that have the page param, usually 'app.request.attributes.get('_route')'
routeParams : additional parameters, usually 'app.request.attributes.get('_route_params')'
totalPages : total number of pages, usually '(entities|length / entitiesPerPage)|ceil'
currentPage : the current page number, usually 'app.request.get('currentPage')'
browserSize : number of elements to be displayed in the browser, usually '4'
Optionnaly, you may customise some stuff :
Change the "pageParameter" variable to change the GET parameter name (default : "currentPage").
Change the labels or add them to your translations :
pagebrowser.firstPage : "First page anchor"
pagebrowser.prevPage : "Previous page anchor"
pagebrowser.nPages : "Some pages..."
pagebrowser.xPage : "A page anchor"
pagebrowser.currentPage: "Current page anchor"
pagebrowser.lastPage: "Last page anchor"
pagebrowser.nextPage: "Next page anchor"
*}
{%- macro pagebrowser(route, routeParams, totalPages, currentPage, browserSize) -%}
{% set pageParameter = 'currentPage' %}
{% set currentPage = (currentPage is defined and not currentPage is null ? currentPage : 0) -%}
{% set browserSize = (browserSize is defined and not browserSize is null ? browserSize : 6) -%}
{% set pagesAfter = (browserSize // 2) %}
{% set pagesBefore = browserSize - pagesAfter - 1 %}
{% if (currentPage-pagesBefore)<0 %}
{% set pagesBefore = currentPage %}
{% set pagesAfter = browserSize - pagesBefore - 1 %}
{%if (currentPage + pagesAfter) >= totalPages %}
{% set pagesAfter = totalPages - currentPage - 1 %}
{% endif %}
{% elseif (currentPage + pagesAfter) >= totalPages %}
{% set pagesAfter = totalPages - currentPage - 1 %}
{% set pagesBefore = browserSize - pagesAfter - 1 %}
{%if (currentPage-pagesBefore) < 0 %}
{% set pagesBefore = currentPage %}
{% endif %}
{% endif -%}
{% if currentPage >= 0 and currentPage < totalPages %}
<ul class="pagination">
{%- if currentPage != 0 %}
<li class="previous">
<a href="{{ path(route, routeParams|merge({(pageParameter): 0})) }}">{{'pagebrowser.firstPage'|trans()}}</a>
</li>
<li class="previous">
<a href="{{ path(route, routeParams|merge({(pageParameter): currentPage - 1})) }}">{{'pagebrowser.prevPage'|trans()}}</a>
</li>
{% else %}
<li class="disabled previous">
<a href="#">{{'pagebrowser.firstPage'|trans()}}</a>
</li>
<li class="disabled previous">
<a href="#">{{'pagebrowser.prevPage'|trans()}}</a>
</li>
{% endif -%}
{%- if currentPage - pagesBefore > 0 -%}
<li class="disabled"><a href="#">{{'pagebrowser.nPages'|trans()}}</a></li>
{%- endif -%}
{%if pagesBefore > 0 %}
{% for p in 0..(pagesBefore-1) %}
{% set pid = currentPage - (pagesBefore - p)%}
<li>
<a href="{{ path(route, routeParams|merge({(pageParameter): pid})) }}">{{'pagebrowser.xPage'|trans()|format(pid+1)}}</a>
</li>
{% endfor %}
{% endif -%}
{#<li><a href="{{ path(route, {'currentPage': currentPage}) }}">{{'pagebrowser.currentPage'|trans()|format(currentPage)}}</a></li>#}
<li class="active"><a href="#">{{'pagebrowser.currentPage'|trans()|format(currentPage + 1)}}</a></li>
{%if pagesAfter > 0 %}
{% for p in 0..(pagesAfter-1) %}
{% set pid = currentPage + p + 1%}
<li>
<a href="{{ path(route, routeParams|merge({(pageParameter): pid})) }}">{{'pagebrowser.xPage'|trans()|format(pid+1)}}</a>
</li>
{% endfor %}
{% endif %}
{%- if currentPage + pagesAfter < totalPages-1 -%}
<li class="disabled"><a href="#">{{'pagebrowser.nPages'|trans()}}</a></li>
{%- endif -%}
{% if currentPage != totalPages-1 %}
<li class="next">
<a href="{{ path(route, routeParams|merge({(pageParameter): currentPage + 1})) }}">{{'pagebrowser.nextPage'|trans()}}</a>
</li>
<li class="next">
<a href="{{ path(route, routeParams|merge({(pageParameter): totalPages - 1})) }}">{{'pagebrowser.lastPage'|trans()}}</a>
</li>
{% else %}
<li class="disabled next">
<a href="#">{{'pagebrowser.nextPage'|trans()}}</a>
</li>
<li class="disabled next">
<a href="#">{{'pagebrowser.lastPage'|trans()}}</a>
</li>
{% endif %}
</ul>
{% endif -%}
{%- endmacro %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment