Opened 19 years ago

Closed 19 years ago

#1127 closed defect (duplicate)

ifequal template tag does not accept hardcoded strings containing spaces

Reported by: elcio@… Owned by: Adrian Holovaty
Component: Template system Version: dev
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

This works:

{% ifequal test "test" %}
    foo...
{% endifequal %}

This not:

{% ifequal test "test 2" %}
    foo...
{% endifequal %}

Suggested patch:

django/trunk/django/core/defaulttags.py
===================================================================
--- defaulttags.py      (revision 1782)
+++ defaulttags.py      (workcopy)
@@ -479,7 +479,16 @@
             ...
         {% endifnotequal %}
     """
-    bits = token.contents.split()
+    tmpbits = token.contents.split()
+    bits=[]
+    sep=""
+    for i in tmpbits:
+       if(sep):
+           bits[-1]+=" "+i
+           if(i[-1]==sep):sep=""
+       else:
+           bits.append(i)
+           if(i[0]=="'" or i[0]=='"'):sep=i
     if len(bits) != 3:
         raise TemplateSyntaxError, "%r takes two arguments" % bits[0]
     end_tag = 'end' + bits[0]

I'm afraid this is not an elegant solution. I think it must to be done in other template tags that uses token.contents.split() and are supposed to accept hardcoded strings. I think django/trunk/tests/othertests/templates.py must be also changed.

Change History (7)

comment:1 by anonymous, 19 years ago

priority: normalhigh
Severity: normalcritical

comment:2 by Armin Ronacher <armin.ronacher@…>, 19 years ago

Hm. Not so good patch. I would split it using this code:

from re import split
bits = split(r'\s+', token.contents, 1)
if len(bits) != 2 or (bits[1][:1] not in '"\'' or bits[1][-1:] not in '"\'':
    raise TemplateSyntaxError, "%r takes two arguments" % bits[0]

Otherwise you will loose double spaces.

comment:3 by hugo, 19 years ago

Actually there is a miniparser for template tag parameters in the code that can be used to parse stuff that might contain spaces. It is used in django.templatetags.i18n for example in the blocktrans tag.

comment:4 by Karsu, 19 years ago

invalid syntax (line 3)

Fixed code:

from re import split
bits = split(r'\s+', token.contents, 1)
if len(bits) != 2 or bits[1][:1] not in '"\'' or bits[1][-1:] not in '"\'':
    raise TemplateSyntaxError, "%r takes two arguments" % bits[0]

comment:5 by Adrian Holovaty, 19 years ago

priority: highnormal
Severity: criticalnormal

comment:6 by Chris Beaven, 19 years ago

Patch #1522 would fix this

comment:7 by Adrian Holovaty, 19 years ago

Resolution: duplicate
Status: newclosed

Closed in favor of #1522.

Note: See TracTickets for help on using tickets.
Back to Top