Django has a useful {% spaceless %}
tag that strips extra whitespace from HTML.
My templates are producing a lot of whitespace and it's too much of a pain to add the {%-
and -%}
everywhere to do whitespace control. Has anyone seen a filter like {% spaceless %}
for Jinja, or maybe {% htmltidy %}
, so that I could view clean HTML while developing?
Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code. Jinja. Original author(s) Armin Ronacher.
Jinja2 allows us to manually control generation of whitespaces. You do it by using a minus sing - to strip whitespaces from blocks, comments or variable expressions. You need to add it to the start or end of given expression to remove whitespaces before or after the block, respectively.
{{ }} tells the template to print the value, this won't work in expressions like you're trying to do. Instead, use the {% set %} template tag and then assign the value the same way you would in normal python code. It was great explanation and simple one.
There is a jinja2 extension that achieves this effect, authored by the jinja2 developer
https://github.com/mitsuhiko/jinja2-htmlcompress
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