Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Changed resolve_variable to resolve integers and floats as integers a…

…nd floats. Added ifequal unit tests. Refs #959

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1690 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e296de5500dd95cff52e70e0c4229134e0e49f7e 1 parent 6768301
@adrianholovaty adrianholovaty authored
Showing with 22 additions and 2 deletions.
  1. +8 −2 django/core/template/__init__.py
  2. +14 −0 tests/othertests/templates.py
View
10 django/core/template/__init__.py
@@ -629,7 +629,7 @@ def resolve_variable(path, context):
"""
Returns the resolved variable, which may contain attribute syntax, within
the given context. The variable may be a hard-coded string (if it begins
- and ends with single or double quote marks).
+ and ends with single or double quote marks), or an integer or float literal.
>>> c = {'article': {'section':'News'}}
>>> resolve_variable('article.section', c)
@@ -645,7 +645,13 @@ def resolve_variable(path, context):
(The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.')
"""
- if path[0] in ('"', "'") and path[0] == path[-1]:
+ if path[0] in '0123456789':
+ number_type = '.' in path and float or int
+ try:
+ current = number_type(path)
+ except ValueError:
+ current = ''
+ elif path[0] in ('"', "'") and path[0] == path[-1]:
current = path[1:-1]
else:
current = context
View
14 tests/othertests/templates.py
@@ -164,6 +164,20 @@ def method(self):
'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),
+ # Integers
+ 'ifequal11': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {}, "no"),
+ 'ifequal12': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": 1}, "yes"),
+ 'ifequal13': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": "1"}, "no"),
+ 'ifequal14': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": 1}, "no"),
+ 'ifequal15': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": "1"}, "yes"),
+
+ # Floats
+ 'ifequal16': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {}, "no"),
+ 'ifequal17': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "yes"),
+ 'ifequal18': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "no"),
+ 'ifequal19': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "no"),
+ 'ifequal20': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "yes"),
+
### IFNOTEQUAL TAG ########################################################
'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),
Please sign in to comment.
Something went wrong with that request. Please try again.