Django

Code

Changeset 3112

Show
Ignore:
Timestamp:
06/07/06 23:29:10 (2 years ago)
Author:
adrian
Message:

Added django.template.Token.split_contents() and used it to add support for strings with spaces in {% ifchanged %}

Files:

Legend:

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

    r3108 r3112  
    503503        {% endifnotequal %} 
    504504    """ 
    505     bits = token.contents.split(
     505    bits = list(token.split_contents()
    506506    if len(bits) != 3: 
    507507        raise TemplateSyntaxError, "%r takes two arguments" % bits[0] 
  • django/trunk/django/template/__init__.py

    r3098 r3112  
    5757import re 
    5858from inspect import getargspec 
    59 from django.utils.functional import curry 
    6059from django.conf import settings 
    6160from django.template.context import Context, RequestContext, ContextPopException 
     61from django.utils.functional import curry 
     62from django.utils.text import smart_split 
    6263 
    6364__all__ = ('Template', 'Context', 'RequestContext', 'compile_string') 
     
    164165 
    165166    def __str__(self): 
    166         return '<%s token: "%s...">' % ( 
    167             {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type], 
    168             self.contents[:20].replace('\n', '') 
    169             ) 
    170  
    171     def __repr__(self): 
    172         return '<%s token: "%s">' % ( 
    173             {TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type], 
    174             self.contents[:].replace('\n', '') 
    175             ) 
     167        return '<%s token: "%s...">' % \ 
     168            ({TOKEN_TEXT: 'Text', TOKEN_VAR: 'Var', TOKEN_BLOCK: 'Block'}[self.token_type], 
     169            self.contents[:20].replace('\n', '')) 
     170 
     171    def split_contents(self): 
     172        return smart_split(self.contents) 
    176173 
    177174class Lexer(object): 
     
    368365            e.source = token.source 
    369366 
    370  
    371367def lexer_factory(*args, **kwargs): 
    372368    if settings.TEMPLATE_DEBUG: 
     
    380376    else: 
    381377        return Parser(*args, **kwargs) 
    382  
    383378 
    384379class TokenParser: 
     
    565560        provided = list(provided) 
    566561        plen = len(provided) 
    567         (args, varargs, varkw, defaults) = getargspec(func) 
     562        args, varargs, varkw, defaults = getargspec(func) 
    568563        # First argument is filter input. 
    569564        args.pop(0) 
     
    821816 
    822817    def simple_tag(self,func): 
    823         (params, xx, xxx, defaults) = getargspec(func) 
     818        params, xx, xxx, defaults = getargspec(func) 
    824819 
    825820        class SimpleNode(Node): 
     
    838833    def inclusion_tag(self, file_name, context_class=Context, takes_context=False): 
    839834        def dec(func): 
    840             (params, xx, xxx, defaults) = getargspec(func) 
     835            params, xx, xxx, defaults = getargspec(func) 
    841836            if takes_context: 
    842837                if params[0] == 'context': 
  • django/trunk/tests/othertests/templates.py

    r3108 r3112  
    307307    'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"), 
    308308    'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"), 
     309 
     310    # SMART SPLITTING 
     311    'ifequal-split01': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {}, "no"), 
     312    'ifequal-split02': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'foo'}, "no"), 
     313    'ifequal-split03': ('{% ifequal a "test man" %}yes{% else %}no{% endifequal %}', {'a': 'test man'}, "yes"), 
     314    'ifequal-split04': ("{% ifequal a 'test man' %}yes{% else %}no{% endifequal %}", {'a': 'test man'}, "yes"), 
     315    'ifequal-split05': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': ''}, "no"), 
     316    'ifequal-split06': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': 'i "love" you'}, "yes"), 
     317    'ifequal-split07': ("{% ifequal a 'i \"love\" you' %}yes{% else %}no{% endifequal %}", {'a': 'i love you'}, "no"), 
     318    'ifequal-split08': (r"{% ifequal a 'I\'m happy' %}yes{% else %}no{% endifequal %}", {'a': "I'm happy"}, "yes"), 
     319    'ifequal-split09': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slash\man"}, "yes"), 
     320    'ifequal-split10': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slashman"}, "no"), 
    309321 
    310322    ### IFNOTEQUAL TAG ########################################################