Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #625 -- Added template-tag decorators simple_tag and inclusion_…

…tag. Taken from new-admin. Thanks, rjwittams

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1410 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c5e5c0fc3573f050d2db08517f88db2e16379455 1 parent 84eb80b
Adrian Holovaty authored November 25, 2005

Showing 1 changed file with 67 additions and 0 deletions. Show diff stats Hide diff stats

  1. 67  django/core/template/decorators.py
67  django/core/template/decorators.py
... ...
@@ -0,0 +1,67 @@
  1
+from django.core.template import Context, Node, TemplateSyntaxError, register_tag, resolve_variable
  2
+from django.core.template_loader import get_template
  3
+from django.utils.functional import curry
  4
+from inspect import getargspec
  5
+
  6
+def generic_tag_compiler(params, defaults, name, node_class, parser, token):
  7
+    "Returns a template.Node subclass."
  8
+    bits = token.contents.split()[1:]
  9
+    bmax = len(params)
  10
+    def_len = defaults and len(defaults) or 0
  11
+    bmin = bmax - def_len
  12
+    if(len(bits) < bmin or len(bits) > bmax):
  13
+        if bmin == bmax:
  14
+            message = "%s takes %s arguments" % (name, bmin)
  15
+        else:
  16
+            message = "%s takes between %s and %s arguments" % (name, bmin, bmax)
  17
+        raise TemplateSyntaxError, message
  18
+    return node_class(bits)
  19
+
  20
+def simple_tag(func):
  21
+    (params, xx, xxx, defaults) = getargspec(func)
  22
+
  23
+    class SimpleNode(Node):
  24
+        def __init__(self, vars_to_resolve):
  25
+            self.vars_to_resolve = vars_to_resolve
  26
+
  27
+        def render(self, context):
  28
+            resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
  29
+            return func(*resolved_vars)
  30
+
  31
+    compile_func = curry(generic_tag_compiler, params, defaults, func.__name__, SimpleNode)
  32
+    compile_func.__doc__ = func.__doc__
  33
+    register_tag(func.__name__, compile_func)
  34
+    return func
  35
+
  36
+def inclusion_tag(file_name, context_class=Context, takes_context=False):
  37
+    def dec(func):
  38
+        (params, xx, xxx, defaults) = getargspec(func)
  39
+        if takes_context:
  40
+            if params[0] == 'context':
  41
+                params = params[1:]
  42
+            else:
  43
+                raise TemplateSyntaxError, "Any tag function decorated with takes_context=True must have a first argument of 'context'"
  44
+
  45
+        class InclusionNode(Node):
  46
+            def __init__(self, vars_to_resolve):
  47
+                self.vars_to_resolve = vars_to_resolve
  48
+
  49
+            def render(self, context):
  50
+                resolved_vars = [resolve_variable(var, context) for var in self.vars_to_resolve]
  51
+                if takes_context:
  52
+                    args = [context] + resolved_vars
  53
+                else:
  54
+                    args = resolved_vars
  55
+
  56
+                dict = func(*args)
  57
+
  58
+                if not getattr(self, 'nodelist', False):
  59
+                    t = get_template(file_name)
  60
+                    self.nodelist = t.nodelist
  61
+                return self.nodelist.render(context_class(dict))
  62
+
  63
+        compile_func = curry(generic_tag_compiler, params, defaults, func.__name__, InclusionNode)
  64
+        compile_func.__doc__ = func.__doc__
  65
+        register_tag(func.__name__, compile_func)
  66
+        return func
  67
+    return dec

0 notes on commit c5e5c0f

Please sign in to comment.
Something went wrong with that request. Please try again.