Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
custom paginator view used in blog post http://zrashwani.com/pagination-optimization-symfony2-doctrine
{% set first = 1%}
{% set previous = max(range(1, current-1)) %}
{% set last = pagesNumber %}
{% set next = min( last, current+1 ) %}
{% set pages_range = range(max(first, current-3), min(last, current+3)) %}
{% if pagesNumber > 1 %}
{%set first = 1 %}
{%set last = pagesNumber %}
{%set query= {} %}
{%set pageParameterName = 'page' %}
<div class="pagination">
{% if first is defined and current != first %}
<span class="first">
<a href="{{ path(route, query|merge({(pageParameterName): first})) }}">&lt;&lt;</a>
</span>
{% endif %}
{% if previous is defined and previous != 0 %}
<span class="previous">
<a href="{{ path(route, query|merge({(pageParameterName): previous})) }}">&lt;</a>
</span>
{% endif %}
{% for page in pages_range %}
{% if page != current %}
<span class="page">
<a href="{{ path(route, query|merge({(pageParameterName): page})) }}">{{ page }}</a>
</span>
{% else %}
<span class="current">{{ page }}</span>
{% endif %}
{% endfor %}
{% if next is defined %}
<span class="next">
<a href="{{ path(route, query|merge({(pageParameterName): next})) }}">&gt;</a>
</span>
{% endif %}
{% if last is defined and current != last %}
<span class="last">
<a href="{{ path(route, query|merge({(pageParameterName): last})) }}">&gt;&gt;</a>
</span>
{% endif %}
</div>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.