Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
nalzok opened this issue Jul 15, 2017 · 5 comments · Fixed by #1014
Closed

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

nalzok opened this issue Jul 15, 2017 · 5 comments · Fixed by #1014

Comments

@nalzok
Copy link

@nalzok nalzok commented Jul 15, 2017

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
@nalzok nalzok changed the title Cannot use plus sign for whitespace control - issue 1 Cannot use the plus sign(+) for whitespace control: tag name expected Jul 15, 2017
@pallets pallets deleted a comment from monspid Jul 27, 2017
@lhoss

This comment has been minimized.

Copy link

@lhoss lhoss commented Jan 25, 2018

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
@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Jan 25, 2018

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.

@davidism

This comment has been minimized.

Copy link
Member

@davidism davidism commented Jan 25, 2018

related to #749
related to #750

@ricky-undeadcoders

This comment has been minimized.

Copy link

@ricky-undeadcoders ricky-undeadcoders commented May 19, 2018

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())
@markroth8

This comment has been minimized.

Copy link
Contributor

@markroth8 markroth8 commented May 31, 2019

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.

davidism added a commit to markroth8/jinja that referenced this issue Jul 20, 2019
…ixes pallets#748.
davidism added a commit that referenced this issue Jul 20, 2019
Allow '{%+' to be valid syntax (but NOP) when lstrip_blocks == False. Fixes #748
andreyv added a commit to andreyv/cms that referenced this issue Aug 20, 2019
The lstrip_blocks environment parameter affects Jinja parsing
mode, see pallets/jinja#748. Hence we need to use the same
whitespace control parameters as in the actual CMS code.

Fixes cms-dev#1096.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Linked pull requests

Successfully merging a pull request may close this issue.

5 participants
You can’t perform that action at this time.