Django

Code

Ticket #1127 (closed: duplicate)

Opened 3 years ago

Last modified 3 years ago

ifequal template tag does not accept hardcoded strings containing spaces

Reported by: elcio@elcio.com.br Assigned to: adrian
Milestone: Component: Template system
Version: SVN Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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.

Attachments

Change History

12/28/05 12:42:15 changed by anonymous

  • priority changed from normal to high.
  • severity changed from normal to critical.

02/02/06 22:42:10 changed by Armin Ronacher <armin.ronacher@active-4.com>

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.

02/03/06 06:20:39 changed by hugo

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.

02/03/06 07:27:40 changed by Karsu

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]

02/03/06 09:03:08 changed by adrian

  • priority changed from high to normal.
  • severity changed from critical to normal.

03/19/06 20:17:02 changed by SmileyChris

Patch #1522 would fix this

05/04/06 23:23:29 changed by adrian

  • status changed from new to closed.
  • resolution set to duplicate.

Closed in favor of #1522.


Add/Change #1127 (ifequal template tag does not accept hardcoded strings containing spaces)




Change Properties
Action