Skip to content

Instantly share code, notes, and snippets.

@amcosta
Forked from SimonSimCity/pagination.html.twig
Created January 28, 2017 13:02
Show Gist options
  • Select an option

  • Save amcosta/04b3a39ea7b912768706598740383a71 to your computer and use it in GitHub Desktop.

Select an option

Save amcosta/04b3a39ea7b912768706598740383a71 to your computer and use it in GitHub Desktop.

Revisions

  1. @SimonSimCity SimonSimCity revised this gist Jan 22, 2013. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions pagination.html.twig
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,7 @@
    * 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 %}
    @@ -32,6 +33,8 @@
    {% 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 })) }}"
    @@ -51,6 +54,8 @@
    {% endfor %}

    <a href="{{ path(paginationPath, currentFilters|merge({page: currentPage+1})) }}">Next</a>
    {% elseif showAlwaysFirstAndLast %}
    <span class="disabled">Next</span>
    {% endif %}
    </div>
    {% endif %}
  2. @SimonSimCity SimonSimCity revised this gist Jan 22, 2013. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions pagination.html.twig
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    {#
    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
  3. @SimonSimCity SimonSimCity revised this gist Jan 22, 2013. 1 changed file with 4 additions and 5 deletions.
    9 changes: 4 additions & 5 deletions pagination.html.twig
    Original file line number Diff line number Diff line change
    @@ -27,16 +27,16 @@
    {% endif %}

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

    <a href="{{ path(paginationPath, currentFilters|merge({ page: currentPage })) }}"
    id="active_page"><b>{{ currentPage }}</b></a>
    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})) }}"><span style="color: green">{{ i }}</span></a>
    <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>
    {% endfor %}

    {% if (lastPage - extremePagesLimit) > (currentPage + nearbyPagesLimit) %}
    @@ -47,8 +47,7 @@
    <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}">{{ i }}</a>
    {% endfor %}

    <a
    href="{{ path(paginationPath, currentFilters|merge({page: currentPage+1})) }}">Next</a>
    <a href="{{ path(paginationPath, currentFilters|merge({page: currentPage+1})) }}">Next</a>
    {% endif %}
    </div>
    {% endif %}
  4. @SimonSimCity SimonSimCity created this gist Jan 22, 2013.
    55 changes: 55 additions & 0 deletions pagination.html.twig
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    {#
    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
    * 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})) }}"><span style="color: green">{{ i }}</span></a>
    {% endfor %}
    {% endif %}

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

    {% if currentPage < lastPage %}
    {% for i in range(currentPage+1, currentPage + nearbyPagesLimit) if ( i <= lastPage ) %}
    <a href="{{ path(paginationPath, currentFilters|merge({page: i})) }}"><span style="color: green">{{ i }}</span></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>
    {% endif %}
    </div>
    {% endif %}
    {% endspaceless %}