From 7db24dd2b080f727cd9753161e88faa308514aad Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 23 Mar 2009 09:42:51 +0000 Subject: [PATCH] Fixed #5756, #6296 -- Most template tags can now handle filters in arguments. Most of the hard work for this was done by akaihola. There are still possibly some problems with the i18n template tags, since they are written quite differently and a few other bug fixes have to made before they can be fixed. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10119 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/template/defaulttags.py | 55 +++++++++++------------- tests/regressiontests/templates/tests.py | 9 +++- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 091130956d..14f6532645 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -39,7 +39,7 @@ class CommentNode(Node): class CycleNode(Node): def __init__(self, cyclevars, variable_name=None): - self.cycle_iter = itertools_cycle([Variable(v) for v in cyclevars]) + self.cycle_iter = itertools_cycle(cyclevars) self.variable_name = variable_name def render(self, context): @@ -70,14 +70,11 @@ class FilterNode(Node): class FirstOfNode(Node): def __init__(self, vars): - self.vars = map(Variable, vars) + self.vars = vars def render(self, context): for var in self.vars: - try: - value = var.resolve(context) - except VariableDoesNotExist: - continue + value = var.resolve(context) if value: return smart_unicode(value) return u'' @@ -169,7 +166,7 @@ class IfChangedNode(Node): def __init__(self, nodelist_true, nodelist_false, *varlist): self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false self._last_seen = None - self._varlist = map(Variable, varlist) + self._varlist = varlist self._id = str(id(self)) def render(self, context): @@ -180,7 +177,7 @@ class IfChangedNode(Node): if self._varlist: # Consider multiple parameters. This automatically behaves # like an OR evaluation of the multiple variables. - compare_to = [var.resolve(context) for var in self._varlist] + compare_to = [var.resolve(context, True) for var in self._varlist] else: compare_to = self.nodelist_true.render(context) except VariableDoesNotExist: @@ -200,7 +197,7 @@ class IfChangedNode(Node): class IfEqualNode(Node): def __init__(self, var1, var2, nodelist_true, nodelist_false, negate): - self.var1, self.var2 = Variable(var1), Variable(var2) + self.var1, self.var2 = var1, var2 self.nodelist_true, self.nodelist_false = nodelist_true, nodelist_false self.negate = negate @@ -208,14 +205,8 @@ class IfEqualNode(Node): return "" def render(self, context): - try: - val1 = self.var1.resolve(context) - except VariableDoesNotExist: - val1 = None - try: - val2 = self.var2.resolve(context) - except VariableDoesNotExist: - val2 = None + val1 = self.var1.resolve(context, True) + val2 = self.var2.resolve(context, True) if (self.negate and val1 != val2) or (not self.negate and val1 == val2): return self.nodelist_true.render(context) return self.nodelist_false.render(context) @@ -371,11 +362,10 @@ class URLNode(Node): args = [arg.resolve(context) for arg in self.args] kwargs = dict([(smart_str(k,'ascii'), v.resolve(context)) for k, v in self.kwargs.items()]) - - + # Try to look up the URL twice: once given the view name, and again - # relative to what we guess is the "main" app. If they both fail, - # re-raise the NoReverseMatch unless we're using the + # relative to what we guess is the "main" app. If they both fail, + # re-raise the NoReverseMatch unless we're using the # {% url ... as var %} construct in which cause return nothing. url = '' try: @@ -388,7 +378,7 @@ class URLNode(Node): except NoReverseMatch: if self.asvar is None: raise - + if self.asvar: context[self.asvar] = url return '' @@ -514,12 +504,14 @@ def cycle(parser, token): if len(args) > 4 and args[-2] == 'as': name = args[-1] - node = CycleNode(args[1:-2], name) + values = [parser.compile_filter(arg) for arg in args[1:-2]] + node = CycleNode(values, name) if not hasattr(parser, '_namedCycleNodes'): parser._namedCycleNodes = {} parser._namedCycleNodes[name] = node else: - node = CycleNode(args[1:]) + values = [parser.compile_filter(arg) for arg in args[1:]] + node = CycleNode(values) return node cycle = register.tag(cycle) @@ -594,7 +586,7 @@ def firstof(parser, token): if len(bits) < 1: raise TemplateSyntaxError("'firstof' statement requires at least one" " argument") - return FirstOfNode(bits) + return FirstOfNode([parser.compile_filter(bit) for bit in bits]) firstof = register.tag(firstof) #@register.tag(name="for") @@ -629,10 +621,10 @@ def do_for(parser, token):
  • Sorry, no athletes in this list.
  • {% endfor %}