Skip to content

Instantly share code, notes, and snippets.

@fyulistian
Created March 11, 2020 08:25
Show Gist options
  • Save fyulistian/47b3490c63d6e6e9ebaf00671c17a9b0 to your computer and use it in GitHub Desktop.
Save fyulistian/47b3490c63d6e6e9ebaf00671c17a9b0 to your computer and use it in GitHub Desktop.
Function pagination on twig
{{ _self.pagination(4, 1, 'http://127.0.0.1:8000/admin/config/system/mutual-funds?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 %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment