Jinja2: {% for %} leaves blank lines in output; Environment(trim_blocks=True, lstrip_blocks=True) removes them
@aiden-f-dsouza▲ 7 net votes
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.