Django: Introducing Djade, a template formatter

Happy DjangoCon US 2024 to you. Whilst I am not there, I have adopted the spirit of the season and got to work hacking together a new tool.
Djade is a formatter for Django templates. It applies rules based on the template style guide in Django’s contribution documentation, plus some more. Ryan Cheley put together this section back in February (forum discussion). I enjoyed contributing to this discussion and wrote “I’d love to work on a formatter tool to match the guide”. Well, now I did it :)
Djade has a similar philosophy to Black, the popular Python code formatter:
You can have any colour you like, as long as it’s [d]jade.
Djade has no options, just one consistent style, open to community discussion. It also aims to format only Django template syntax, leaving the surrounding language syntax (HTML, TXT, XML, ...) untouched for safety.
Djade is built in Rust, using a translation of the relevant parts of Django’s template parser. It’s fast: a quick benchmark on my MacBook shows it takes ~20ms to format 377 templates in one project.
Run Djade on your template files directly or with its pre-commit integration. You’ll find it makes changes like:
-{% extends "base.html" %}
+{% extends "base.html" %}
-{% load omelette %}
-
-{% load frittata %}
+{% load frittata omelette %}
{% block content %}
- <h1>{{egg.colour | title}}</h1>
+ <h1>{{ egg.colour|title }}</h1>
-{% endblock %}
+{% endblock content %}
Djade can also apply some fixes based on changes to template tags in past Django versions. For example, it updates the static files library name, deprecated in Django 2.1:
-{% load staticfiles %}
+{% load static %}
See more in the Djade documentation.
Fin
Djade is brand new and probably has bugs. Please try it on your project today and let me know how it goes. I am keen to refine it so that it becomes a “must-have” tool for Django developers.
May your templates be neat,
—Adam
😸😸😸 Check out my new book on using GitHub effectively, Boost Your GitHub DX! 😸😸😸
One summary email a week, no spam, I pinky promise.
Related posts:
- Introducing django-upgrade, a tool for upgrading your Django projects
- Django: Raise an error for missing template variables
- Django: The perils of
string_if_invalidin templates - Django: Introducing django-harlequin, a launcher for Terminal-based SQL IDE Harlequin
Tags: django, pre-commit