lstrip_blocks feature: Smarter whitespace control #139

wants to merge 12 commits into


3 participants

kristi commented Jul 12, 2012

Jinja's whitespace control using {%- and -%} is convenient, but tedious to apply to many blocks to get cleanly indented output.

The new lstrip_blocks option removes spaces and tabs from the beginning of a line up to a block tag. If there are characters other than spaces or tabs preceding the block tag, nothing is stripped. So the spaces would be removed from

    {% set x = 1 %}

but not from

    hello {% set x = 1 %}

Often, a block tag appears on its own line in a template. Using lstrip_blocks and the existing trim_blocks options, the entire tag line will be automatically removed when rendering a template.

Example template:

    {% for post in posts %}
        <li>{{ post.title }}</li>
    {% endfor %}

The rendered output:

        <li>Post One</li>
        <li>Post Two</li>
        <li>Post Three</li>

rmadsen commented Sep 11, 2012

This would be a very useful for ASCII output, where whitespace is significant (for table alignment, etc).


mitsuhiko commented May 19, 2013

I like that, thanks a lot. Merged.

@mitsuhiko mitsuhiko closed this May 19, 2013

akbargumbira referenced this pull request in kartoza/flask_user_map Nov 20, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment