Django

Code

Changeset 1690

Show
Ignore:
Timestamp:
12/15/05 23:33:24 (3 years ago)
Author:
adrian
Message:

Changed resolve_variable to resolve integers and floats as integers and floats. Added ifequal unit tests. Refs #959

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/template/__init__.py

    r1497 r1690  
    630630    Returns the resolved variable, which may contain attribute syntax, within 
    631631    the given context. The variable may be a hard-coded string (if it begins 
    632     and ends with single or double quote marks)
     632    and ends with single or double quote marks), or an integer or float literal
    633633 
    634634    >>> c = {'article': {'section':'News'}} 
     
    646646    (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') 
    647647    """ 
    648     if path[0] in ('"', "'") and path[0] == path[-1]: 
     648    if path[0] in '0123456789': 
     649        number_type = '.' in path and float or int 
     650        try: 
     651           current = number_type(path) 
     652        except ValueError: 
     653           current = '' 
     654    elif path[0] in ('"', "'") and path[0] == path[-1]: 
    649655        current = path[1:-1] 
    650656    else: 
  • django/trunk/tests/othertests/templates.py

    r1586 r1690  
    164164    'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"), 
    165165    'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"), 
     166 
     167    # Integers 
     168    'ifequal11': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {}, "no"), 
     169    'ifequal12': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": 1}, "yes"), 
     170    'ifequal13': ('{% ifequal a 1 %}yes{% else %}no{% endifequal %}', {"a": "1"}, "no"), 
     171    'ifequal14': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": 1}, "no"), 
     172    'ifequal15': ('{% ifequal a "1" %}yes{% else %}no{% endifequal %}', {"a": "1"}, "yes"), 
     173 
     174    # Floats 
     175    'ifequal16': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {}, "no"), 
     176    'ifequal17': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "yes"), 
     177    'ifequal18': ('{% ifequal a 1.2 %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "no"), 
     178    'ifequal19': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": 1.2}, "no"), 
     179    'ifequal20': ('{% ifequal a "1.2" %}yes{% else %}no{% endifequal %}', {"a": "1.2"}, "yes"), 
    166180 
    167181    ### IFNOTEQUAL TAG ########################################################