mirror of https://github.com/django/django.git
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
This commit is contained in:
parent
005e70387c
commit
def5d10ffc
|
@ -353,7 +353,8 @@ def get_filters_from_token(token):
|
||||||
def resolve_variable(path, context):
|
def resolve_variable(path, context):
|
||||||
"""
|
"""
|
||||||
Returns the resolved variable, which may contain attribute syntax, within
|
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'}}
|
>>> c = {'article': {'section':'News'}}
|
||||||
>>> resolve_variable('article.section', c)
|
>>> resolve_variable('article.section', c)
|
||||||
|
@ -369,30 +370,33 @@ def resolve_variable(path, context):
|
||||||
|
|
||||||
(The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
|
(The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
|
||||||
"""
|
"""
|
||||||
current = context
|
if path[0] in ('"', "'") and path[0] == path[-1]:
|
||||||
bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
|
current = path[1:-1]
|
||||||
while bits:
|
else:
|
||||||
try: # dictionary lookup
|
current = context
|
||||||
current = current[bits[0]]
|
bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR)
|
||||||
except (TypeError, AttributeError, KeyError):
|
while bits:
|
||||||
try: # attribute lookup
|
try: # dictionary lookup
|
||||||
current = getattr(current, bits[0])
|
current = current[bits[0]]
|
||||||
if callable(current):
|
except (TypeError, AttributeError, KeyError):
|
||||||
if getattr(current, 'alters_data', False):
|
try: # attribute lookup
|
||||||
current = ''
|
current = getattr(current, bits[0])
|
||||||
else:
|
if callable(current):
|
||||||
try: # method call (assuming no args required)
|
if getattr(current, 'alters_data', False):
|
||||||
current = current()
|
|
||||||
except SilentVariableFailure:
|
|
||||||
current = ''
|
current = ''
|
||||||
except TypeError: # arguments *were* required
|
else:
|
||||||
current = '' # invalid method call
|
try: # method call (assuming no args required)
|
||||||
except (TypeError, AttributeError):
|
current = current()
|
||||||
try: # list-index lookup
|
except SilentVariableFailure:
|
||||||
current = current[int(bits[0])]
|
current = ''
|
||||||
except (IndexError, ValueError, KeyError):
|
except TypeError: # arguments *were* required
|
||||||
raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute
|
current = '' # invalid method call
|
||||||
del bits[0]
|
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
|
return current
|
||||||
|
|
||||||
def resolve_variable_with_filters(var_string, context):
|
def resolve_variable_with_filters(var_string, context):
|
||||||
|
|
|
@ -110,6 +110,12 @@ TEMPLATE_TESTS = {
|
||||||
'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
|
'ifequal02': ("{% ifequal a b %}yes{% endifequal %}", {"a": 1, "b": 1}, "yes"),
|
||||||
'ifequal03': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 2}, "no"),
|
'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"),
|
'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 ########################################################
|
### IFNOTEQUAL TAG ########################################################
|
||||||
'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
|
'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
|
||||||
|
|
Loading…
Reference in New Issue