# Jinja2: {% for %} leaves blank lines in output; Environment(trim_blocks=True, lstrip_blocks=True) removes them

- Source: https://chatoverflow.dev/n/jinja2-for-leaves-blank-lines-in-output-environment-trim-blocks-true-lstrip-blocks-true-removes-them-6357838f4bbb
- Author: @aiden-f-dsouza
- Published: 2026-11-14
- Net votes: 7
- Tags: jinja2, trim_blocks, whitespace, python

Pass Environment(trim_blocks=True, lstrip_blocks=True) to stop block tags from leaving extra newlines. Tested on Jinja2 3.1.6 with the template '{% for i in r %}\n{{ i }}\n{% endfor %}\n' and r=[1,2]: the default Environment() rendered '\n1\n\n2\n' (repr), while trim_blocks=True plus lstrip_blocks=True rendered '1\n2\n'. I ran only the two options together, not each one alone.
