Code

Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#8902 closed (duplicate)

tags having problems with filtered variables using arguments with spacing

Reported by: kire Owned by: nobody
Component: Template system Version: 1.0
Severity: Keywords: template tags
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

If you want to do something like this:
{% with activity.start_date|date:"l j F" as date1 %}
You'll get this error:
Exception Value: u'with' expected format is 'value as name'
This is due to this code:

    bits = list(token.split_contents())
    if len(bits) != 4 or bits[2] != "as":
        raise TemplateSyntaxError("%r expected format is 'value as name'" %
                                  bits[0])

First of all, maybe another bug: split_contents gave me a double "" on a filter as |cut:" ", so I replaced it with contents.split() which worked fine for me.
Then, this error: len(bits) !=4 is actually overkill. The tag works like this: 0 -> tagname, 1-> a variable with filter, 2 -> as, 3 -> newvar. Actually you should not check if the ' '-splitted content is 4 long, you should check if [1] passes the compile_filter, which will throw it's own error.

I tried to write a regex or iteration that really checked if you did really passed 1 variable + filter(s), but that really isn't trivial, and the compile_filter function's error is actually clear enough.

I added a patch + test for the {% with %} tag only at the moment (because that's where I encountered the problem). However, other tags have the problem also.

Attachments (1)

with_patch_test.diff (1.9 KB) - added by kire 6 years ago.
Patch + tests

Download all attachments as: .zip

Change History (4)

Changed 6 years ago by kire

Patch + tests

comment:1 Changed 6 years ago by kire

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

To be more precise, actually every tag that blindly splits on spaces will break on filter arguments with spaces in it. Such a patch would be quite large (all default tags, special tags & contrip apps tags).

Of course, Since the with tags works with my patch you can always work around this problem in other tags.

comment:2 Changed 6 years ago by dc

  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #6271.

comment:3 Changed 5 years ago by anonymous

  • milestone post-1.0 deleted

Milestone post-1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.