From: jose Date: Wed, 20 Apr 2011 18:39:02 +0000 (-0400) Subject: Made the paginate tag a normal tag with an optional argument, template, to override... X-Git-Tag: release-2.0~9^2 X-Git-Url: https://git.mdrn.pl/django-pagination.git/commitdiff_plain/ef08a2061f1f41deae227e3cb5beaceb7e17dd23?hp=--cc Made the paginate tag a normal tag with an optional argument, template, to override the default template --- ef08a2061f1f41deae227e3cb5beaceb7e17dd23 diff --git a/pagination/templatetags/pagination_tags.py b/pagination/templatetags/pagination_tags.py index dd338a2..d7f1884 100644 --- a/pagination/templatetags/pagination_tags.py +++ b/pagination/templatetags/pagination_tags.py @@ -7,6 +7,8 @@ from django import template from django.http import Http404 from django.core.paginator import Paginator, InvalidPage from django.conf import settings +from django.template.loader import select_template +from django.template import Context register = template.Library() @@ -103,7 +105,47 @@ class AutoPaginateNode(template.Node): context['paginator'] = paginator context['page_obj'] = page_obj return u'' + + +class PaginateNode(template.Node): + + def __init__(self, template=None): + self.template = template + + def render(self, context): + template_list = ['pagination/pagination.html'] + to_return = paginate(context) + + if self.template: + template_list.insert(0, self.template) + + t = select_template(template_list) + if not t: + return None + context = Context(to_return) + return t.render(context) + +def do_paginate(parser, token): + """ + {% paginate [using] [template] %} + + {% paginate %} + {% paginate using paginations/custom_pagination.html %} + """ + argv = token.contents.split() + argc = len(argv) + + if argc > 3: + raise template.TemplateSyntaxError, "Tag %s takes at most 2 argument." % argv[0] + + if argc == 1: + return PaginateNode() + if argc == 3 and argv[1] == 'using': + return PaginateNode(template=argv[2]) + + raise template.TemplateSyntaxError, "Tag %s is invalid. Please check the syntax" % argv[0] + def paginate(context, window=DEFAULT_WINDOW, hashtag=''): """ @@ -226,6 +268,7 @@ def paginate(context, window=DEFAULT_WINDOW, hashtag=''): except (KeyError, AttributeError): return {} -register.inclusion_tag('pagination/pagination.html', takes_context=True)( - paginate) +#register.inclusion_tag('pagination/pagination.html', takes_context=True)( +# paginate) +register.tag('paginate', do_paginate) register.tag('autopaginate', do_autopaginate) diff --git a/setup.py b/setup.py index 5718e27..7a4de48 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup, find_packages -version = '1.0.9' +version = '1.1.0' LONG_DESCRIPTION = """ How to use django-pagination