Pass the template context to the pagination template
[django-pagination.git] / linaro_django_pagination / templatetags / pagination_tags.py
index 815cb7f..b447625 100644 (file)
@@ -40,8 +40,10 @@ from django.template import (
     TOKEN_BLOCK,
     TemplateSyntaxError,
     Variable,
+    loader,
 )
 from django.template.loader import select_template
+from django.utils.text import unescape_string_literal
 
 # TODO, import this normally later on
 from linaro_django_pagination.settings import *
@@ -92,8 +94,8 @@ def do_autopaginate(parser, token):
     if queryset_var is None:
         raise TemplateSyntaxError(
             "Invalid syntax. Proper usage of this tag is: "
-            "{%% autopaginate QUERYSET [PAGINATE_BY] [ORPHANS]"
-            " [as CONTEXT_VAR_NAME] %%}")
+            "{% autopaginate QUERYSET [PAGINATE_BY] [ORPHANS]"
+            " [as CONTEXT_VAR_NAME] %}")
     return AutoPaginateNode(queryset_var, multiple_paginations, paginate_by, orphans, context_var)
 
 
@@ -184,29 +186,33 @@ class PaginateNode(Node):
         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)
+        return loader.render_to_string(template_list, to_return, 
+            context_instance = context)
+
 
 
 def do_paginate(parser, token):
     """
-    {% paginate [using] [template] %}
+    Emits the pagination control for the most recent autopaginate list
+
+    Syntax is:
 
-    {% paginate %}
-    {% paginate using paginations/custom_pagination.html %}
+        paginate [using "TEMPLATE"]
+
+    Where TEMPLATE is a quoted template name. If missing the default template
+    is used (paginate/pagination.html).
     """
-    argv = token.contents.split()
+    argv = token.split_contents()
     argc = len(argv)
-    if argc > 3:
-        raise 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 TemplateSyntaxError("Tag %s is invalid. Please check the syntax" % argv[0])
+        template = None
+    elif argc == 3 and argv[1] == 'using':
+        template = unescape_string_literal(argv[2])
+    else:
+        raise TemplateSyntaxError(
+            "Invalid syntax. Proper usage of this tag is: "
+            "{% paginate [using \"TEMPLATE\"] %}")
+    return PaginateNode(template)
 
 
 def paginate(context, window=DEFAULT_WINDOW, margin=DEFAULT_MARGIN):