Opened 19 years ago
Closed 19 years ago
#1127 closed defect (duplicate)
ifequal template tag does not accept hardcoded strings containing spaces
Reported by: | 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 , 19 years ago
priority: | normal → high |
---|---|
Severity: | normal → critical |
comment:2 by , 19 years ago
comment:3 by , 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 , 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 , 19 years ago
priority: | high → normal |
---|---|
Severity: | critical → normal |
Hm. Not so good patch. I would split it using this code:
Otherwise you will loose double spaces.