diff options
| author | Russell Keith-Magee <russell@keith-magee.com> | 2006-07-04 03:21:44 +0000 |
|---|---|---|
| committer | Russell Keith-Magee <russell@keith-magee.com> | 2006-07-04 03:21:44 +0000 |
| commit | 6b383afd39ee2e493a308cb5c70dfa880d27edf6 (patch) | |
| tree | 85068dd1c4626d68da862814bd5d2cfd804098c8 /django | |
| parent | 009f224e5799ffd963ce325fd1c1181bfab3dc14 (diff) | |
Fixes #1338, Refs #1400, #2237 -- Modified variable resolution to allow template 'if' statements to work if TEMPLATE_STRING_IF_INVALID is set. Modified unit tests to force the use of this variable, so that returning '' isn't confused with an actual failure.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@3268 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/template/__init__.py | 9 | ||||
| -rw-r--r-- | django/template/context.py | 4 | ||||
| -rw-r--r-- | django/template/defaulttags.py | 19 |
3 files changed, 22 insertions, 10 deletions
diff --git a/django/template/__init__.py b/django/template/__init__.py index 08f433fec9..993de7304e 100644 --- a/django/template/__init__.py +++ b/django/template/__init__.py @@ -318,7 +318,7 @@ class Parser(object): self.tags.update(lib.tags) self.filters.update(lib.filters) - def compile_filter(self,token): + def compile_filter(self, token): "Convenient wrapper for FilterExpression" return FilterExpression(token, self) @@ -543,11 +543,14 @@ class FilterExpression(object): raise TemplateSyntaxError, "Could not parse the remainder: %s" % token[upto:] self.var, self.filters = var, filters - def resolve(self, context): + def resolve(self, context, ignore_failures=False): try: obj = resolve_variable(self.var, context) except VariableDoesNotExist: - obj = settings.TEMPLATE_STRING_IF_INVALID + if ignore_failures: + return None + else: + return settings.TEMPLATE_STRING_IF_INVALID for func, args in self.filters: arg_vals = [] for lookup, arg in args: diff --git a/django/template/context.py b/django/template/context.py index 44a97f95a8..6d9efdc7ec 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -37,7 +37,7 @@ class Context(object): for d in self.dicts: if d.has_key(key): return d[key] - return settings.TEMPLATE_STRING_IF_INVALID + raise KeyError(key) def __delitem__(self, key): "Delete a variable from the current context" @@ -49,7 +49,7 @@ class Context(object): return True return False - def get(self, key, otherwise): + def get(self, key, otherwise=None): for d in self.dicts: if d.has_key(key): return d[key] diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 88cb5f68be..0a4fe33d82 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -45,7 +45,10 @@ class FirstOfNode(Node): def render(self, context): for var in self.vars: - value = resolve_variable(var, context) + try: + value = resolve_variable(var, context) + except VariableDoesNotExist: + continue if value: return str(value) return '' @@ -144,8 +147,14 @@ class IfEqualNode(Node): return "<IfEqualNode>" def render(self, context): - val1 = resolve_variable(self.var1, context) - val2 = resolve_variable(self.var2, context) + try: + val1 = resolve_variable(self.var1, context) + except VariableDoesNotExist: + val1 = None + try: + val2 = resolve_variable(self.var2, context) + except VariableDoesNotExist: + val2 = None 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) @@ -177,7 +186,7 @@ class IfNode(Node): if self.link_type == IfNode.LinkTypes.or_: for ifnot, bool_expr in self.bool_exprs: try: - value = bool_expr.resolve(context) + value = bool_expr.resolve(context, True) except VariableDoesNotExist: value = None if (value and not ifnot) or (ifnot and not value): @@ -186,7 +195,7 @@ class IfNode(Node): else: for ifnot, bool_expr in self.bool_exprs: try: - value = bool_expr.resolve(context) + value = bool_expr.resolve(context, True) except VariableDoesNotExist: value = None if not ((value and not ifnot) or (ifnot and not value)): |
