Skip to content

Commit

Permalink
add support for django jinja
Browse files Browse the repository at this point in the history
  • Loading branch information
defrex committed Jun 23, 2014
1 parent 5637af4 commit 22236cf
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
52 changes: 52 additions & 0 deletions bootstrap_paginator/templates/bootstrap_paginator/macros.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

{% macro paginator(page, get_args={}) %}
{% if page.paginator.num_pages > 1 %}
{% set ADJACENT_PAGES = 5 %}

{% set start_page = page.number - ADJACENT_PAGES %}
{% if start_page < 1 %}
{% set start_page = 1 %}
{% endif %}

{% set end_page = page.number + ADJACENT_PAGES %}
{% if end_page > page.paginator.num_pages %}
{% set end_page = page.paginator.num_pages %}
{% endif %}

<ul class="pagination pagination-centered pagination-sm">

{% if page.has_previous %}
<li><a href="{{ urlencode_plus(get_args, page=page.previous_page_number) }}">&laquo;</a></li>
{% endif %}

{% if start_page > 1 %}
<li><a href="{{ urlencode_plus(get_args, page=1) }}">1</a></span>
{% if start_page > 2 %}
<li class="disabled"><a href="javascript:;">...</a></li>
{% endif %}
{% endif %}

{% for page_number in range(start_page, end_page) %}
{% if page_number == page.number %}
<li class="active"><a href="javascript:;">{{ page.number }}</a></li>
{% else %}
<li><a href="{{ urlencode_plus(get_args, page=page_number) }}">{{ page_number }}</a></li>
{% endif %}
{% endfor %}

{% if end_page < page.paginator.num_pages %}
{% if end_page < page.paginator.num_pages - 1 %}
<li class="disabled"><a href="javascript:;">...</a></li>
{% endif %}
<li><a href="{{ urlencode_plus(get_args, page=page.paginator.num_pages) }}">
{{ page.paginator.num_pages }}
</a></li>
{% endif %}

{% if page.has_next %}
<li><a href="{{ urlencode_plus(get_args, page=page.next_page_number) }}">&raquo;</a></li>
{% endif %}

</ul>
{% endif %}
{% endmacro %}
28 changes: 23 additions & 5 deletions bootstrap_paginator/templatetags/paginator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
from django import template
from django.conf import settings

try:
from django_jinja import library
lib = library.Library()

except ImportError:
class LibraryStub(object):
@staticmethod
def global_function(func):
return func
lib = LibraryStub()


PAGINATOR_ADJACENT_PAGES = getattr(settings, 'PAGINATOR_ADJACENT_PAGES', 2)

Expand All @@ -22,13 +33,13 @@ def paginator(context, page=None):
page = context.get('page_obj', page)
paginator = page.paginator

startPage = page.number - PAGINATOR_ADJACENT_PAGES
if startPage <= PAGINATOR_ADJACENT_PAGES + 1:
startPage = 1
endPage = page.number + PAGINATOR_ADJACENT_PAGES + 1
start_page = page.number - PAGINATOR_ADJACENT_PAGES
if start_page <= PAGINATOR_ADJACENT_PAGES + 1:
start_page = 1
end_page = page.number + PAGINATOR_ADJACENT_PAGES + 1

page_numbers = [
n for n in range(startPage, endPage)
n for n in range(start_page, end_page)
if n >= 1 and n <= paginator.num_pages
]

Expand All @@ -50,3 +61,10 @@ def append_to_get(context, **kwargs):
get = {}
get.update(kwargs)
return '?{}'.format(urlencode(get))


@lib.global_function
def urlencode_plus(values, **plus):
values = values.copy()
values.update(plus)
return '?{}'.format(urlencode(values))

0 comments on commit 22236cf

Please sign in to comment.