summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2010-12-19 15:00:50 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2010-12-19 15:00:50 +0000
commit314fabc930a1bb361ca06e9c948bb726ad8df99a (patch)
tree973d32f9ec0f1885ecd72d8fe45132fb4987b897 /django
parent7adffaeaf6dfa22db0b6b2a29632b9150c7ac732 (diff)
Fixed #14908 -- Added a 'takes_context' argument to simple_tag. Thanks to Julien Phalip for driving the issue and providing the final patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14987 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
-rw-r--r--django/template/base.py43
1 files changed, 31 insertions, 12 deletions
diff --git a/django/template/base.py b/django/template/base.py
index d934e050ee..2a1d8be1ce 100644
--- a/django/template/base.py
+++ b/django/template/base.py
@@ -872,21 +872,40 @@ class Library(object):
self.filters[getattr(func, "_decorated_function", func).__name__] = func
return func
- def simple_tag(self,func):
- params, xx, xxx, defaults = getargspec(func)
+ def simple_tag(self, func=None, takes_context=None):
+ def dec(func):
+ params, xx, xxx, defaults = getargspec(func)
+ if takes_context:
+ if params[0] == 'context':
+ params = params[1:]
+ else:
+ raise TemplateSyntaxError("Any tag function decorated with takes_context=True must have a first argument of 'context'")
- class SimpleNode(Node):
- def __init__(self, vars_to_resolve):
- self.vars_to_resolve = map(Variable, vars_to_resolve)
+ class SimpleNode(Node):
+ def __init__(self, vars_to_resolve):
+ self.vars_to_resolve = map(Variable, vars_to_resolve)
- def render(self, context):
- resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
- return func(*resolved_vars)
+ def render(self, context):
+ resolved_vars = [var.resolve(context) for var in self.vars_to_resolve]
+ if takes_context:
+ func_args = [context] + resolved_vars
+ else:
+ func_args = resolved_vars
+ return func(*func_args)
- compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
- compile_func.__doc__ = func.__doc__
- self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
- return func
+ compile_func = curry(generic_tag_compiler, params, defaults, getattr(func, "_decorated_function", func).__name__, SimpleNode)
+ compile_func.__doc__ = func.__doc__
+ self.tag(getattr(func, "_decorated_function", func).__name__, compile_func)
+ return func
+
+ if func is None:
+ # @register.simple_tag(...)
+ return dec
+ elif callable(func):
+ # @register.simple_tag
+ return dec(func)
+ else:
+ raise TemplateSyntaxError("Invalid arguments provided to simple_tag")
def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
def dec(func):