Created
March 13, 2014 02:49
-
-
Save Artistan/9521105 to your computer and use it in GitHub Desktop.
Pagination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
You need the following variables: | |
paginationCurrentFilters: an associative array that contains the current settings of the page like search, filters, etc. For example {% set paginationCurrentFilters = { ‘search’: search, ‘filter_by_is_active’: filter_by_is_active } %}, so it can be easy for us to merge the pagination with these filters. | |
paginationCurrentPage : the current page you are in | |
paginationRouteName : the route name of the path that paginates files | |
paginationTotal : The paginationTotal number of pages (ceil(found_entities/items_per_page)) | |
paginationLastPage : represents the paginationTotal number of existing pages | |
paginationExtremePagesLimit : the number of first and last pages to be displayed | |
paginationNearbyPagesLimit : the number of pages that are displayed around the active page | |
#} | |
{% if paginationLastPage > 1 %} | |
{% if not paginationExtremePagesLimit %} | |
{% set paginationExtremePagesLimit = 3 %} | |
{% endif %} | |
{% if not paginationNearbyPagesLimit %} | |
{% set paginationNearbyPagesLimit = 2 %} | |
{% endif %} | |
<div class="pagination"> | |
{% if paginationCurrentPage > 1 %} | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page=0">«</a></li> | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ paginationCurrentPage-1 }}">‹</a></li> | |
{% endif %} | |
{% for i in range(1, paginationExtremePagesLimit) %} | |
{% if ( i < paginationCurrentPage-paginationNearbyPagesLimit ) %} | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ i }}">{{ i }}</a></li> | |
{% endif %} | |
{% endfor %} | |
{% if paginationExtremePagesLimit + 1 < paginationCurrentPage - paginationNearbyPagesLimit %} | |
<li><span class="sep-dots">...</span></li> | |
{% endif %} | |
{% for i in range(paginationCurrentPage-paginationNearbyPagesLimit, paginationCurrentPage-1) %} | |
{% if ( i > 0 ) %} | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ i }}">{{ i }}</a></li> | |
{% endif %} | |
{% endfor %} | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ paginationCurrentPage }}" id="active_page">{{ paginationCurrentPage }}</a></li> | |
{% if paginationCurrentPage < paginationLastPage %} | |
{% for i in range(paginationCurrentPage+1, paginationTotal) %} | |
{% if loop.index <= paginationNearbyPagesLimit and i <= paginationLastPage %} | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ i }}">{{ i }}</a></li> | |
{% endif %} | |
{% endfor %} | |
{% if (paginationLastPage - paginationExtremePagesLimit) > (paginationCurrentPage + paginationNearbyPagesLimit ) %} | |
<li><span class="sep-dots">...</span></li> | |
{% endif %} | |
{% for i in range(paginationLastPage-paginationExtremePagesLimit+1, paginationLastPage) %} | |
{% if ( i > paginationCurrentPage+paginationNearbyPagesLimit ) %} | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ i }}">{{ i }}</a></li> | |
{% endif %} | |
{% endfor %} | |
{% endif %} | |
{% if paginationCurrentPage < paginationLastPage %} | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ (paginationCurrentPage+1) }}">›</a></li> | |
<li><a href="{{ url_route(paginationRouteName, paginationCurrentFilters) }}&page={{ paginationLastPage }}">»</a></li> | |
{% endif %} | |
</div> | |
<div style="clear: both"></div> | |
{% endif %} |
Example usage
{% set paginationCurrentFilters = filters %}
{% set paginationCurrentPage = result.paging.current_page %}
{% set paginationRouteName = 'hardware_search' %}
{% set paginationTotal = result.paging.total_pages %}
{% set paginationLastPage = result.paging.total_pages %}
{% include 'package::share.pagination' %}
And this is setup for bootstrap styles!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a pagination template to be used with twig in a Laravel 4 framework.
Hope it helps someone out there.