Home > flask > [flask] Jinja2: don’t print empty lines

[flask] Jinja2: don’t print empty lines

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:

    {% if True %}
    {% endif %}

the generated output looks like this:




See? Jinja2 litters the output with empty lines. How to get rid of them?

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


Categories: flask Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: