Django

Code

Changeset 587

Show
Ignore:
Timestamp:
08/31/05 21:18:04 (3 years ago)
Author:
adrian
Message:

Fixed #365 -- Changed template.resolve_variable to resolve hard-coded strings. Thanks, davidschein

Files:

Legend:

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

    r443 r587  
    354354    """ 
    355355    Returns the resolved variable, which may contain attribute syntax, within 
    356     the given context. 
     356    the given context. The variable may be a hard-coded string (if it begins 
     357    and ends with single or double quote marks). 
    357358 
    358359    >>> c = {'article': {'section':'News'}} 
     
    370371    (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') 
    371372    """ 
    372     current = context 
    373     bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR) 
    374     while bits: 
    375         try: # dictionary lookup 
    376             current = current[bits[0]] 
    377         except (TypeError, AttributeError, KeyError): 
    378             try: # attribute lookup 
    379                 current = getattr(current, bits[0]) 
    380                 if callable(current): 
    381                     if getattr(current, 'alters_data', False): 
    382                         current = '' 
    383                     else: 
    384                         try: # method call (assuming no args required) 
    385                             current = current() 
    386                         except SilentVariableFailure: 
     373    if path[0] in ('"', "'") and path[0] == path[-1]: 
     374        current = path[1:-1] 
     375    else: 
     376        current = context 
     377        bits = path.split(VARIABLE_ATTRIBUTE_SEPARATOR) 
     378        while bits: 
     379            try: # dictionary lookup 
     380                current = current[bits[0]] 
     381            except (TypeError, AttributeError, KeyError): 
     382                try: # attribute lookup 
     383                    current = getattr(current, bits[0]) 
     384                    if callable(current): 
     385                        if getattr(current, 'alters_data', False): 
    387386                            current = '' 
    388                         except TypeError: # arguments *were* required 
    389                             current = '' # invalid method call 
    390             except (TypeError, AttributeError): 
    391                 try: # list-index lookup 
    392                     current = current[int(bits[0])] 
    393                 except (IndexError, ValueError, KeyError): 
    394                     raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute 
    395         del bits[0] 
     387                        else: 
     388                            try: # method call (assuming no args required) 
     389                                current = current() 
     390                            except SilentVariableFailure: 
     391                                current = '' 
     392                            except TypeError: # arguments *were* required 
     393                                current = '' # invalid method call 
     394                except (TypeError, AttributeError): 
     395                    try: # list-index lookup 
     396                        current = current[int(bits[0])] 
     397                    except (IndexError, ValueError, KeyError): 
     398                        raise VariableDoesNotExist, "Failed lookup for key [%s] in %r" % (bits[0], current) # missing attribute 
     399            del bits[0] 
    396400    return current 
    397401 
  • django/trunk/tests/othertests/templates.py

    r574 r587  
    111111    'ifequal03': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 2}, "no"), 
    112112    'ifequal04': ("{% ifequal a b %}yes{% else %}no{% endifequal %}", {"a": 1, "b": 1}, "yes"), 
     113    'ifequal05': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "test"}, "yes"), 
     114    'ifequal06': ("{% ifequal a 'test' %}yes{% else %}no{% endifequal %}", {"a": "no"}, "no"), 
     115    'ifequal07': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "test"}, "yes"), 
     116    'ifequal08': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {"a": "no"}, "no"), 
     117    'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"), 
     118    'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"), 
    113119 
    114120    ### IFNOTEQUAL TAG ########################################################