telekommander
3/26/2016 - 11:24 AM

A gist for pagination in Twig, based on the total number of pages, the current page and some URL-settings.

A gist for pagination in Twig, based on the total number of pages, the current page and some URL-settings.

{#
  Source: http://dev.dbl-a.com/symfony-2-0/symfony2-and-twig-pagination/
  Updated by: Simon Schick <simonsimcity@gmail.com>

  Parameters:
    * currentFilters (array) : associative array that contains the current route-arguments
    * currentPage (int) : the current page you are in
    * paginationPath (string) : the route name to use for links
    * showAlwaysFirstAndLast (bool) : Always show first and last link (just disabled)
    * lastPage (int) : represents the total number of existing pages
#}
{% spaceless %}
    {% if lastPage > 1 %}

        {# the number of first and last pages to be displayed #}
        {% set extremePagesLimit = 3 %}

        {# the number of pages that are displayed around the active page #}
        {% set nearbyPagesLimit = 2 %}

        <div class="pagination">
            {% if currentPage > 1 %}
                <a href="{{ path(paginationPath, currentFilters|merge({page: currentPage-1})) }}">Previous</a>

                {% for i in range(1, extremePagesLimit) if ( i < currentPage - nearbyPagesLimit ) %}
                    <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>
                {% endfor %}

                {% if extremePagesLimit + 1 < currentPage - nearbyPagesLimit %}
                    <span class="sep-dots">...</span>
                {% endif %}

                {% for i in range(currentPage-nearbyPagesLimit, currentPage-1) if ( i > 0 ) %}
                    <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>
                {% endfor %}
            {% elseif showAlwaysFirstAndLast %}
                <span class="disabled">Previous</span>
            {% endif %}

            <a href="{{ path(paginationPath, currentFilters|merge({ page: currentPage })) }}"
               class="active">{{ currentPage }}</a>

            {% if currentPage < lastPage %}
                {% for i in range(currentPage+1, currentPage + nearbyPagesLimit) if ( i <= lastPage ) %}
                    <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>
                {% endfor %}

                {% if  (lastPage - extremePagesLimit) > (currentPage + nearbyPagesLimit) %}
                    <span class="sep-dots">...</span>
                {% endif %}

                {% for i in range(lastPage - extremePagesLimit+1, lastPage) if ( i > currentPage + nearbyPagesLimit ) %}
                    <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>
                {% endfor %}

                <a href="{{ path(paginationPath, currentFilters|merge({page: currentPage+1})) }}">Next</a>
            {% elseif showAlwaysFirstAndLast %}
                <span class="disabled">Next</span>
            {% endif %}
        </div>
    {% endif %}
{% endspaceless %}