Surprising behavior with string comparisons

4 views
Skip to first unread message

Jonathan Mayer

unread,
Oct 1, 2018, 8:10:01 PM10/1/18
to pocoo-libs
I just noticed surprising behavior when comparing a field against string literals.

{%- if foo == "bar" %}

Produces the following error:

jinja2.exceptions.TemplateSyntaxError: expected token 'end of statement block', got 'bar'

I can resolve this problem by replacing the double-quoted string literal with a single-quote string literal.  This works:

{%- if foo == 'bar' %}

Why don't the two kinds of string literals behave the same?

This might be specific to my environment, which is version 2.10 running in a python2.7 environment with utf-8 encoded strings.  I haven't really done my due diligence to make a standalone reproduction case yet, but I'm still throwing my observation onto the list in case anybody else has encountered this before, too.

Jonathan Mayer

unread,
Oct 1, 2018, 8:14:06 PM10/1/18
to pocoo-libs
Ah hah!  I just figured this out.

The problem was that I had an unterminated double-quoted string literal earlier in my template.  Apparently, this doesn't cause an exception, but instead breaks all other double-quoted string literals in my template.  Single-quoted literals are still parsed correctly.

So, maybe the bug report here is: misleading failures as a result of unterminated string literals?
Reply all
Reply to author
Forward
0 new messages