mitsuhiko/jinja2 — Issue #87: trim_blocks not working for a recursive loop
Author: Sirtea
Created: 2012-01-12 19:53:16+00:00
State: open

(I'll put the Flask view code, as I have no idea reproducing the code only with jinja2)

I have a code that renders some elements that are in a tree fashion distribution, and a recursive loop in jinja2 (listed at the bottom). As I activated the trim_blocks, lines with only tags are removed, rather than left empty. No surpise here.
Following the docs (http://jinja.pocoo.org/docs/templates/#whitespace-control), there is no need to use the minus sign to manually supress the newlines.

But when I use the recursive for (http://jinja.pocoo.org/docs/templates/#list-of-control-structures) the white spaces are not supressed as I would, but I can add the minus sign to supress, so this is a VERY minor issue. Look at the code below.

¿Is this behaviour correct?

# view: myapp.py
from flask import Flask, render_template

app = Flask(__name__)
app.jinja_env.trim_blocks = True

@app.route('/')
def hello():
data = [['a', 'b'], 'c', [['d', 'e'], 'f']]
return render_template('hello.html', data=data)

# template buggy: hello.html
{% for element in data recursive %}
{% if element is string %}

{{ element }}

{% else %}
{{ loop(element) }}
{% endif %}
{% endfor %}

# template ok: hello.html
{% for element in data recursive %}
{% if element is string %}

{{ element }}

{% else %}
{{ loop(element) -}}
{% endif %}
{% endfor %}