Opened 19 years ago
Closed 16 years ago
#1522 closed defect (fixed)
[patch] Allow template tags to accept hardcoded strings with spaces
Reported by: | Chris Beaven | Owned by: | nobody |
---|---|---|---|
Component: | Template system | Version: | |
Severity: | normal | Keywords: | |
Cc: | ferringb@… | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Attachments (1)
Change History (14)
by , 19 years ago
Attachment: | smart_token_splitting.diff added |
---|
comment:1 by , 19 years ago
comment:2 by , 19 years ago
+1 - this is a great patch! I was needing this functionality just the other day (especially for creating my own custom tags, but for built-in tags it is just as useful).
comment:4 by , 19 years ago
Good work, Kieran! I still feel this patch has some benefits: it works efficiently to create minimal overheads on the current code and is focussed on solving a specific problem people are having. Your patch obviously takes it to the next level though ;)
Rather than making people read the diff to understand this patch: what this patch does is give the Token class a new function, split_contents. So rather than a simple string split on spaces, it will handle quoted bits. Eg:
{% ifequal test "test 2" %} foo... {% endifequal %}
Here's the function added to the Token class:
def split_contents(self): if check_for_quotes.search(self.contents): return split_token_re.findall(self.contents) else: return self.contents.split()
comment:5 by , 19 years ago
comment:6 by , 19 years ago
This ticket is pretty nice, but the regex doesn't support escaped strings. For example, I'd like to be able to do this:
{% ifequal 'the "person\'s hat"' 'the person\'s hat' %}
Surely a perfect string-splitting regular expression is available on the Web somewhere...
comment:8 by , 19 years ago
In [3112] -- Added django.template.Token.split_contents() and used it to add support for strings with spaces in {% ifchanged %}
comment:9 by , 19 years ago
Here's a patch to use this in decorators like include_tag etc:
-
django/template/__init__.py
749 749 750 750 def generic_tag_compiler(params, defaults, name, node_class, parser, token): 751 751 "Returns a template.Node subclass." 752 bits = token.contents.split()[1:]752 bits = list(token.split_contents())[1:] 753 753 bmax = len(params) 754 754 def_len = defaults and len(defaults) or 0 755 755 bmin = bmax - def_len
This makes it much easier to pass strings with spaces and/or template variables to tags like so:
{% my_tag "hello {{ world }}" %}
regards
Matthew Flanagan
comment:10 by , 18 years ago
Triage Stage: | Unreviewed → Accepted |
---|
Does this work for more than ifchanged?
comment:11 by , 18 years ago
Cc: | added |
---|
comment:13 by , 16 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Here's a test I put together to make sure this wasn't going to consume any more time. Looks like it takes about double the time as a normal .split(), but that's acceptable for the added functionality I think.