Opened 4 years ago

Closed 4 years ago

#15387 closed (duplicate)

Parameter parse_until doesn't compare to tag name.

Reported by: klein4 Owned by: klein4
Component: Template system Version: 1.2
Severity: Keywords: template, template tag, parser
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

django.template.Parser.parse() takes an argument parse_until, which the docs say is a tuple of tag names which indicate that parsing should halt. However, when Parser.parse() finds a new token of token_type=TOKEN_BLOCK, it tests token.contents in parse_until rather than token.contents.split_token()[0] in parse_until. The latter more closely matches the documented functionality.

This distinction is relevant when tokens after the initial tag contain arguments.

To see why this is useful, consider a custom block tag random which randomly chooses a nodelist according to its weight. It might be invoked like this:

{% random "2" %}
This node has a 2/7 chance of being chosen.
{% or "2" %}
This node has a 2/7 chance of being chosen.
{% or "3" %}
This node has a 3/7 chance of being chosen.
{% endrandom %}

As it currently stands, it is impossible to give non-initial tokens arguments as in this example. This may be considered a non-trivial patch, but it seems to align with the documented functionality of the parse_until parameter.

Attachments (1)

django-parse_until.diff (1.2 KB) - added by klein4 4 years ago.
This is my first Django patch. Created with diff -crB from my library root. Apologies if the format is inappropriate.

Download all attachments as: .zip

Change History (4)

Changed 4 years ago by klein4

This is my first Django patch. Created with diff -crB from my library root. Apologies if the format is inappropriate.

comment:1 Changed 4 years ago by klein4

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to klein4
  • Patch needs improvement unset

comment:2 Changed 4 years ago by klein4

Woops. I mistyped the line token.contents.split_token()[0] in parse_until in the description above. That should read token.split_contents()[0] in parse_until. I typed it correctly in my patch, however.

comment:3 Changed 4 years ago by russellm

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

I'm pretty sure this is a duplicate of #3100.

Regarding format - preferred format is the output of "svn diff".

Note: See TracTickets for help on using tickets.
Back to Top