Jinja: Cannot use the plus sign(+) for whitespace control: tag name expected

Created on 15 Jul 2017  ·  5Comments  ·  Source: pallets/jinja

Expected Behavior

Assuming trim_blocks is always False.

When lstrip_blocks is set to True, jinja should output

<!-- 1 -->
    <!-- 2 -->
foo<!-- 3 -->
<!-- 4 -->
<!-- 5 -->

And when it's False, jinja should output

    <!-- 1 -->
    <!-- 2 -->
    foo<!-- 3 -->
    <!-- 4 -->
    <!-- 5 -->

Actual Behavior

In either case,

jinja2.exceptions.TemplateSyntaxError: tag name expected

Template Code

    <!-- 1 -->
    {%+ if True %}<!-- 2 -->
    foo<!-- 3 -->
    {% endif %}<!-- 4 -->
    <!-- 5 -->

The following template also has a similar issue:

    <!-- 1 -->
    {% if True %}<!-- 2 -->
    foo<!-- 3 -->
    {%+ endif %}<!-- 4 -->
    <!-- 5 -->

Full Traceback

Traceback (most recent call last):
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1997, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/sunqingyao/PycharmProjects/microblog/app/views.py", line 22, in index
    posts=posts
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/flask/templating.py", line 133, in render_template
    return _render(ctx.app.jinja_env.get_or_select_template(template_name_or_list),
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/environment.py", line 869, in get_or_select_template
    return self.get_template(template_name_or_list, parent, globals)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/environment.py", line 830, in get_template
    return self._load_template(name, self.make_globals(globals))
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/environment.py", line 804, in _load_template
    template = self.loader.load(self, name, globals)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/loaders.py", line 125, in load
    code = environment.compile(source, name, filename)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/environment.py", line 591, in compile
    self.handle_exception(exc_info, source_hint=source_hint)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "/home/sunqingyao/PycharmProjects/microblog/app/templates/demo.html", line 2, in template
    {%+ if True %}<!-- 2 -->
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/environment.py", line 497, in _parse
    return Parser(self, source, name, encode_filename(filename)).parse()
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/parser.py", line 896, in parse
    result = nodes.Template(self.subparse(), lineno=1)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/parser.py", line 878, in subparse
    rv = self.parse_statement()
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/parser.py", line 125, in parse_statement
    self.fail('tag name expected', token.lineno)
  File "/home/sunqingyao/Envs/flask-mega/lib/python3.6/site-packages/jinja2/parser.py", line 59, in fail
    raise exc(msg, lineno, self.name, self.filename)
jinja2.exceptions.TemplateSyntaxError: tag name expected

Your Environment

  • Python version: 3.6.1+
  • Jinja version: Jinja2==2.9.6

Most helpful comment

We would be happy to fix this. Unfortunately, we maintain this as volunteers with limited free time. We rely on the community to contribute analysis and patches to fill in when we can't. If you are affected by this and are interested in fixing it, please submit a PR.

All 5 comments

Why no motivation to fix this?
Ref: https://stackoverflow.com/questions/35775207/remove-unnecessary-whitespace-from-jinja-rendered-template#35777386
In our case, using a '-+' combo would have been useful in an ansible (jinja2-)template to avoid spaces before {%... (to keep same indenting for intermixed lines of logic and contents ):

<root_element>
  {%- if ... +%}
  <property>

  </property>
  {%- if ... +%}
 strips before

We would be happy to fix this. Unfortunately, we maintain this as volunteers with limited free time. We rely on the community to contribute analysis and patches to fill in when we can't. If you are affected by this and are interested in fixing it, please submit a PR.

related to #749
related to #750

The issue here just appears to be that when lstrip_blocks = False, the syntax "{%+ statement %}" fails with error message "jinja2.exceptions.TemplateSyntaxError: tag name expected".

This is more or less expected behavior because there's nothing to disable if lstrip_blocks is already disabled, but it could fail a little more elegantly.

Example:

This works:

from jinja2 import Environment
env = Environment(lstrip_blocks=True, trim_blocks=False)
str7481 = '''
    <!-- 1 -->
    {%+ if True %}<!-- 2 -->
    foo<!-- 3 -->
    {% endif %}<!-- 4 -->
    <!-- 5 -->'''
str7482 = '''
    <!-- 1 -->
    {% if True %}<!-- 2 -->
    foo<!-- 3 -->
    {%+ endif %}<!-- 4 -->
    <!-- 5 -->'''
print(env.from_string(str7481).render())
print(env.from_string(str7482).render())

This does not:

from jinja2 import Environment
env = Environment(lstrip_blocks=False, trim_blocks=False)
str7481 = '''
    <!-- 1 -->
    {%+ if True %}<!-- 2 -->
    foo<!-- 3 -->
    {% endif %}<!-- 4 -->
    <!-- 5 -->'''
str7482 = '''
    <!-- 1 -->
    {% if True %}<!-- 2 -->
    foo<!-- 3 -->
    {%+ endif %}<!-- 4 -->
    <!-- 5 -->'''
print(env.from_string(str7481).render())
print(env.from_string(str7482).render())

I discussed this issue with @davidism and he suggested that the underlying syntax of templates should not change when lstrip_blocks = False. In other words, '{%+' should still be valid syntax in this case, but the semantics should just be to ignore the '+', since it is redundant. I'm submitting a PR that tests and implements this potential behavior.

Was this page helpful?
0 / 5 - 0 ratings