Archive
Posts Tagged ‘empty lines’
[flask] Jinja2: don’t print empty lines
January 29, 2015
Leave a comment
Problem
When using Flask (or Django), I don’t care much about the generated HTMLs. They may be ugly, who cares. However, there is one thing that bothers me. When I write this for instance in a template:
<div>
{% if True %}
yay
{% endif %}
</div>
the generated output looks like this:
<div>
yay
</div>
See? Jinja2 litters the output with empty lines. How to get rid of them?
Solution
The official documentation talks about this here. It says you need to enable both trim_blocks and lstrip_blocks. In Flask, you can do that like this:
... app = Flask(__name__) app.jinja_env.trim_blocks = True app.jinja_env.lstrip_blocks = True ...
Done.
Categories: flask
blank lines, empty lines, jinja2
