Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added django.template.Token.split_contents() and used it to add suppo…

…rt for strings with spaces in {% ifchanged %}

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3112 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5edd1335b2ae3530bab124b8e9cfb6928a975088 1 parent 0f0560a
Adrian Holovaty authored June 08, 2006
25  django/template/__init__.py
@@ -56,9 +56,10 @@
56 56
 """
57 57
 import re
58 58
 from inspect import getargspec
59  
-from django.utils.functional import curry
60 59
 from django.conf import settings
61 60
 from django.template.context import Context, RequestContext, ContextPopException
  61
+from django.utils.functional import curry
  62
+from django.utils.text import smart_split
62 63
 
63 64
 __all__ = ('Template', 'Context', 'RequestContext', 'compile_string')
64 65
 
@@ -163,16 +164,12 @@ def __init__(self, token_type, contents):
163 164
         self.token_type, self.contents = token_type, contents
164 165
 
165 166
     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  
-            )
  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 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  
-            )
  171
+    def split_contents(self):
  172
+        return smart_split(self.contents)
176 173
 
177 174
 class Lexer(object):
178 175
     def __init__(self, template_string, origin):
@@ -367,7 +364,6 @@ def compile_function_error(self, token, e):
367 364
         if not hasattr(e, 'source'):
368 365
             e.source = token.source
369 366
 
370  
-
371 367
 def lexer_factory(*args, **kwargs):
372 368
     if settings.TEMPLATE_DEBUG:
373 369
         return DebugLexer(*args, **kwargs)
@@ -380,7 +376,6 @@ def parser_factory(*args, **kwargs):
380 376
     else:
381 377
         return Parser(*args, **kwargs)
382 378
 
383  
-
384 379
 class TokenParser:
385 380
     """
386 381
     Subclass this and implement the top() method to parse a template line. When
@@ -564,7 +559,7 @@ def resolve(self, context):
564 559
     def args_check(name, func, provided):
565 560
         provided = list(provided)
566 561
         plen = len(provided)
567  
-        (args, varargs, varkw, defaults) = getargspec(func)
  562
+        args, varargs, varkw, defaults = getargspec(func)
568 563
         # First argument is filter input.
569 564
         args.pop(0)
570 565
         if defaults:
@@ -820,7 +815,7 @@ def filter_function(self, func):
820 815
         return func
821 816
 
822 817
     def simple_tag(self,func):
823  
-        (params, xx, xxx, defaults) = getargspec(func)
  818
+        params, xx, xxx, defaults = getargspec(func)
824 819
 
825 820
         class SimpleNode(Node):
826 821
             def __init__(self, vars_to_resolve):
@@ -837,7 +832,7 @@ def render(self, context):
837 832
 
838 833
     def inclusion_tag(self, file_name, context_class=Context, takes_context=False):
839 834
         def dec(func):
840  
-            (params, xx, xxx, defaults) = getargspec(func)
  835
+            params, xx, xxx, defaults = getargspec(func)
841 836
             if takes_context:
842 837
                 if params[0] == 'context':
843 838
                     params = params[1:]
2  django/template/defaulttags.py
@@ -502,7 +502,7 @@ def do_ifequal(parser, token, negate):
502 502
             ...
503 503
         {% endifnotequal %}
504 504
     """
505  
-    bits = token.contents.split()
  505
+    bits = list(token.split_contents())
506 506
     if len(bits) != 3:
507 507
         raise TemplateSyntaxError, "%r takes two arguments" % bits[0]
508 508
     end_tag = 'end' + bits[0]
12  tests/othertests/templates.py
@@ -307,6 +307,18 @@ def method(self):
307 307
     'ifequal09': ('{% ifequal a "test" %}yes{% else %}no{% endifequal %}', {}, "no"),
308 308
     'ifequal10': ('{% ifequal a b %}yes{% else %}no{% endifequal %}', {}, "yes"),
309 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"),
  321
+
310 322
     ### IFNOTEQUAL TAG ########################################################
311 323
     'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"),
312 324
     'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),

0 notes on commit 5edd133

Please sign in to comment.
Something went wrong with that request. Please try again.