Opened 8 years ago

Closed 8 years ago

#27022 closed Bug (wontfix)

some DjangoTemplates builtin tags misparse lack of whitespace between variable name and boolean operator in arguments

Reported by: Peter Lai Owned by: nobody
Component: Template system Version: 1.10
Severity: Normal Keywords: tags, arguments, operators, whitespace
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The django.DjangoTemplates template backend is unable to parse a comparison operator when there is no whitespace around the operator using builtin tag if:

{% if foo=='bar' %}
<!-- do something -->
{% endif %}

raises:

TemplateSyntaxError at url

Could not parse the remainder: '=='bar'' from 'foo=='bar''

At least one space is required on both sides of the == operator (So foo =='bar' and foo== 'bar' throws Could not parse the remainder: '=='bar'' from '=='bar'' and Could not parse the remainder: '==' from 'foo==', respectively). More than one space is acceptable.

This seems to affect the following boolean operators: ==, !=, <, >, <=, >=. My guess is the boolean expression tokenizer in DjangoTemplates first tries split(' ') then ultimately evals [0][1][2] (but I haven't actually looked at the source to verify this).

Change History (4)

comment:1 by Baptiste Mispelon, 8 years ago

Triage Stage: UnreviewedAccepted

Hi,

Indeed, the template language doesn't seem to support that. I'm not sure if it ever did either (I went back as far as 1.6 and observed the same error).

It'd be nice to be able to make this use-case work, so that we can match the Python semantics a bit better.
However, if it's too tricky then this should be mentionned in the documentation [1] at least.

Thanks.

[1] https://docs.djangoproject.com/en/dev/ref/templates/builtins/#boolean-operators

comment:2 by Tim Graham, 8 years ago

As for me, I'd rather not let it work so as to enforce a consistent style in templates. Since all examples use this style, I don't see much need to document that omitting the whitespace doesn't work. Is it difficult to figure out what went wrong from the error message?

in reply to:  2 comment:3 by Anton Samarchyan, 8 years ago

Replying to Tim Graham:

As for me, I'd rather not let it work so as to enforce a consistent style in templates. Since all examples use this style, I don't see much need to document that omitting the whitespace doesn't work. Is it difficult to figure out what went wrong from the error message?

I completely agree. Probably wontfix is the best option here.

comment:4 by Tim Graham, 8 years ago

Resolution: wontfix
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top