Dear all,
I would like to take this opportunity to announce my side project to
the Django community. It's a Django template indenter called DjHTML.
It's Black for Django templates!
https://github.com/rtts/djhtml/
Over the past year I have fallen in love with Black and pre-commit to
automatically format code upon each commit. However, I could not find
any tool that would do the same for HTML files and so I decided to
write one myself. Take for instance the following template:
{% block content %}
<form
{% if index > 1 %}
class="extra"
{% endif %}
>
{% localize on %}
{% if clickable %}
<a href="{% url 'detail' %}">
{% endif %}
Blog post {{ nr }}
{% if clickable %}
</a>
{% endif %}
{% endlocalize %}
</form>
{% endblock %}
This is what it looks like after processing by DjHTML:
{% block content %}
<form
{% if index > 1 %}
class="extra"
{% endif %}
>
{% localize on %}
{% if clickable %}
<a href="{% url 'detail' %}">
{% endif %}
Blog post {{ nr }}
{% if clickable %}
</a>
{% endif %}
{% endlocalize %}
</form>
{% endblock %}
Currently, DjHTML is able to parse DTL, HTML, CSS, Javascript and any
combinations of these inside the same template. The indentation rules
are similar to Emacs' web-mode.el but with better handling of some
edge cases.
The difference with Black is that DjHTML is an indenter and not a
formatter: It will only add/remove whitespace and not insert newlines
or any other characters. The goal is to indent already well-structured
templates but not to fix broken ones. Also, unlike Black, the tabwidth
is configurable :-)
I am hoping that some of you will give it a try and hopefully provide
me with some feedback, test cases, and bug reports that will help me
improve this tool.
Greetings,
Jaap Joris Vens