token.split_contents() behavior

179 views
Skip to first unread message

Tom von Schwerdtner

unread,
Nov 6, 2009, 9:28:57 PM11/6/09
to Django users
This confuses me a bit, so I thought I'd see if anyone knows why this
works the way it does.

If I have this custom tag:

{% foo bar baz %}

token.split_contents() will give me three strings:

['foo', 'bar', 'baz']

Yet if I have:

{% foo "bar baz" %}

token.split_contents() will give two strings, the second of which
includes the quotes:

['foo', '"bar baz"']

Is that the intended behavior? It seems a bit wonky...

-Tom

Tomasz Zieliński

unread,
Nov 7, 2009, 12:33:02 PM11/7/09
to Django users
On 7 Lis, 03:28, Tom von Schwerdtner <tomv...@gmail.com> wrote:

>
> Is that the intended behavior? It seems a bit wonky...
>

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-the-compilation-function

"""
if not (format_string[0] == format_string[-1] and format_string[0] in
('"', "'")):
raise template.TemplateSyntaxError, "%r tag's argument should
be in quotes" % tag_name
"""

"""
The token.split_contents() method separates the arguments on spaces
while keeping quoted strings together. The more straightforward
token.contents.split() wouldn't be as robust, as it would naively
split on all spaces, including those within quoted strings. It's a
good idea to always use token.split_contents().
"""

So - yes, it is intended

--
Tomasz Zieliński
http://pyconsultant.eu
Reply all
Reply to author
Forward
0 new messages