Opened 15 years ago
Closed 15 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: | no | UI/UX: | no |
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)
Change History (4)
by , 15 years ago
| Attachment: | django-parse_until.diff added |
|---|
comment:1 by , 15 years ago
| Owner: | changed from to |
|---|
comment:2 by , 15 years ago
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 by , 15 years ago
| Resolution: | → duplicate |
|---|---|
| Status: | new → closed |
I'm pretty sure this is a duplicate of #3100.
Regarding format - preferred format is the output of "svn diff".
This is my first Django patch. Created with diff -crB from my library root. Apologies if the format is inappropriate.