Quantcast
Channel: Recent Gists from devnix
Viewing all articles
Browse latest Browse all 13

Example of pagination with Twig

0
0
_pagination.html.twig
{#
Parameters:
* total (int): number of pages
* current (int): current pages
* url (string): route name & query (string): route parameter
ex: list/page-5?q=myFilter (5 = page and query = myFilter)
* nearbyPagesLimit (int) optional: limit of pages around the current page
#}
{% macro pagination(total, current, url, nearbyPagesLimit = 4) %}
{% spaceless %}
{% if total > 1 %}
<ul class="pagination">
{% for i in 1..total %}
{% if 0 == (current - nearbyPagesLimit) - loop.index %}
<li><a href="{{ (url ~ 1)|e }}">1</a></li>
{% if 1 != loop.index %}
<li><span>…</span></li>
{% endif %}
{% elseif 0 == (current + nearbyPagesLimit) - loop.index and (current + nearbyPagesLimit) < total %}
<li><span>…</span></li>
{% elseif 0 < (current - nearbyPagesLimit) - loop.index %}
{% elseif 0 > (current + nearbyPagesLimit) - loop.index %}
{% else %}
<li {{ current == loop.index ? 'class="active"' }}>
{% if current == loop.index %}
<span>{{ loop.index }}</span>
{% else %}
<a href="{{ url ~ loop.index }}">{{ loop.index }}</a>
{% endif %}
</li>
{% endif %}
{% endfor %}
{% if current != total and (current + nearbyPagesLimit) < total %}
<li><a href="{{ (url ~ total)|e }}">{{ total }}</a></li>
{% endif %}
</ul>
{% endif %}
{% endspaceless %}
{% endmacro %}

Viewing all articles
Browse latest Browse all 13

Latest Images

Trending Articles





Latest Images