summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-09-01 02:18:04 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-09-01 02:18:04 +0000
commitdef5d10ffc57a2764ec4260c145a19ddec698a23 (patch)
tree9c5b1de88f044b9b3660a55234e3e3a748f6c0d3
parent005e70387cad0adec94af84227a5964bb988e76d (diff)
Fixed #365 -- Changed template.resolve_variable to resolve hard-coded strings. Thanks, davidschein
git-svn-id: http://code.djangoproject.com/svn/django/trunk@587 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/core/template.py52
-rw-r--r--tests/othertests/templates.py6
2 files changed, 34 insertions, 24 deletions
diff --git a/django/core/template.py b/django/core/template.py
index 4459323e0c..b7faebd70c 100644
--- a/django/core/template.py
+++ b/django/core/template.py
@@ -353,7 +353,8 @@ def get_filters_from_token(token):
def resolve_variable(path, context):
"""
Returns the resolved variable, which may contain attribute syntax, within
- the given context.
+ the given context. The variable may be a hard-coded string (if it begins
+ and ends with single or double quote marks).
>>> c = {'article': {'section':'News'}}
>>> resolve_variable('article.section', c)
@@ -369,30 +370,33 @@ def resolve_variable(path, context):
(The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
"""
- current = context
- bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
- while bits:
- try: # dictionary lookup
- current = current[bits[0]]
- except (TypeError, AttributeError, KeyError):
- try: # attribute lookup
- current = getattr(current, bits[0])
- if callable(current):
- if getattr(current, 'alters_data', False):
- current = ''
- else:
- try: # method call (assuming no args required)
- current = current()
- except SilentVariableFailure:
+ if path[0] in ('"', "'") and path[0] == path[-1]:
+ current = path[1:-1]
+ else:
+ current = context
+ bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
+ while bits:
+ try: # dictionary lookup
+ current = current[bits[0]]
+ except (TypeError, AttributeError, KeyError):
+ try: # attribute lookup
+ current = getattr(current, bits[0])
+ if callable(current):
+ if getattr(current, 'alters_data', False):
current = ''
- except TypeError: # arguments *were* required
- current = '' # invalid method call
- except (TypeError, AttributeError):
- try: # list-index lookup
- current = current[int(bits[0])]
- except (IndexError, ValueError, KeyError):
- raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute
- del bits[0]
+ else:
+ try: # method call (assuming no args required)
+ current = current()
+ except SilentVariableFailure:
+ current = ''
+ except TypeError: # arguments *were* required
+ current = '' # invalid method call
+ except (TypeError, AttributeError):
+ try: # list-index lookup
+ current = current[int(bits[0])]
+ except (IndexError, ValueError, KeyError):
+ raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute
+ del bits[0]
return current
def resolve_variable_with_filters(var_string, context):
diff --git a/tests/othertests/templates.py b/tests/othertests/templates.py
index ecc1d6484f..01b77db401 100644
--- a/tests/othertests/templates.py
+++ b/tests/othertests/templates.py
@@ -110,6 +110,12 @@ TEMPLATE_TESTS = {
'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
'ifequal03': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 2}, "no"),
'ifequal04': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 1}, "yes"),
+ 'ifequal05': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "test"}, "yes"),
+ 'ifequal06': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "no"}, "no"),
+ 'ifequal07': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "test"}, "yes"),
+ 'ifequal08': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "no"}, "no"),
+ 'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
+ 'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),
### IFNOTEQUAL TAG ########################################################
'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),