We are using jinja2 to create our html but, because of the many loops and other things we do in jinja to produce the html, the html 'looks' ugly....(note: this is just for aesthetics). Is there anything we can do to clean up the html? (Other than the obvious of cleaning up our jinja2 code, which would make our template somewhat unreadable to us staffers)
Something like beautiful soup's prettify?
(Yes, I realize this question is a pretty nit-picky question...the ocd in me says to clean it up).
for instance:
<table>
<tbody>
<tr>
<td>
a column
</td>
<td>
a value
</td>
</tr>
</tbody>
</table>
Pretty ugly, eeh?
Add '-' to the tags:
{%- if 'this'=='this' -%}
{{ blah }}
{%- endif -%}
It looks like someone out there created a library to do just what need. See this library which I found attached to this question (whom you should upvote).
You can also configure Jinja to replace tags with nothing (instead of an empty line) by setting trim_blocks
and lstrip_blocks
to True
. For example, in a Flask app, you might write:
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
The documentation explains whitespace control further.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With