26

In my init.py file I have:

app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True

I expect in my jinja2 template that whitespace will be trimmed, so that:

<div>
{% if x == 3 %}
<small>{{ x }}</small>
{% endif %}
</div>

will render as:

<div>
<small>3</small>
</div>

Instead, I get extra whitespace:

<div>

<small>3</small>

</div>

Why doesn't trim_blocks and lstrip_blocks trim the whitespace?

Yeo
  • 11,416
  • 6
  • 63
  • 90
user_78361084
  • 3,538
  • 22
  • 85
  • 147
  • 1
    I was not able to reproduce it (Jinja 2.7.3). Viewing the rendered source in Firefox and Chrome for me shows the template rendering correctly without the extra newlines. There are several possible causes here. It could be a jinja2 issue, but it could also be an editor or linux-windows issue if you are developing in one platform and viewing in another (for example http://superuser.com/questions/374028/how-are-n-and-r-handled-differently-on-linux-and-windows) – tohster Feb 10 '15 at 08:07
  • I second @tohster's comment. Are you placing the assignment statements in the wrong place? Can you post complete source? – Tony Beta Lambda Nov 19 '15 at 05:01

3 Answers3

15

It seems like your environment settings are not set before jinja2 loads your template.

class jinja2.Environment([options])

... Instances of this class may be modified if they are not shared and if no template was loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior.

http://jinja.pocoo.org/docs/dev/api/#jinja2.Environment

Check the order/structure of your code to see how the environment settings vs templates are loaded.

As an aside, jinja2's whitespace control does work as expected without the complexity of environments and loading:

import jinja2

template_string = '''<div>
{% if x == 3 %}
<small>{{ x }}</small>
{% endif %}
</div>
'''
# create templates
template1 = jinja2.Template(template_string)
template2 = jinja2.Template(template_string, trim_blocks=True)

# render with and without settings
print template1.render(x=3)
print '\n<!-- {} -->\n'.format('-' * 32)
print template2.render(x=3)

<div>

<small>3</small>

</div>

<!-- -------------------------------- -->

<div>
<small>3</small>
</div>

I've not used jinja2, but after scanning the docs, loading order seems to be suspect.

tmthydvnprt
  • 10,398
  • 8
  • 52
  • 72
1

You have to escape the {% if %} and {% endif %} statements with a minus sign in order to suppress the empty lines :

<div>
{%- if x == 3 %}
<small>{{ x }}</small>
{%- endif %}
</div>
Philippe
  • 21
  • 1
  • 2
    according to http://jinja.pocoo.org/docs/dev/templates/#whitespace-control if you configure Jinja2 to trim_blocks and lstrip_blocks it should suppress the newlines by default. So you are not answering the OP's question (he tries to set those options but is still not working) – cowbert Jun 29 '17 at 20:29
0

if your code is in macro.html, you must add "-" manually and couldn't use the following code.

app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
teng wang
  • 29
  • 3