lstrip_blocks feature: Smarter whitespace control #139

Closed
wants to merge 12 commits into
from

Projects

None yet

3 participants

@kristi
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:

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

The rendered output:

<ul>
        <li>Post One</li>
        <li>Post Two</li>
        <li>Post Three</li>
</ul>
@rmadsen
rmadsen commented Sep 11, 2012

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

@mitsuhiko
Member

I like that, thanks a lot. Merged.

@mitsuhiko mitsuhiko closed this May 19, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment