Skip to content

Django templates

Django template support is experimental. HTML files containing {{, {% or {# automatically use the Django pack. .djhtml always selects it. Detection uses the supplied contents for stdin and unsaved editor buffers.

rainbow-fmt diff templates/account.html
rainbow-fmt format templates/

Template blocks with line breaks around their bodies are indented together with the HTML around them.

{% block content %}
<ul>
{% for person in people %}
<li>{{ person.name }}</li>
{% empty %}
<li>Nobody here.</li>
{% endfor %}
</ul>
{% endblock %}

becomes

{% block content %}
    <ul>
        {% for person in people %}
            <li>{{ person.name }}</li>
        {% empty %}
            <li>Nobody here.</li>
        {% endfor %}
    </ul>
{% endblock %}

Formatting and configuration

The pack recognizes if/elif/else, for/empty, block, with, autoescape, ifchanged, cache, localization blocks and partialdef. Custom blocks using matching {% name %} and {% endname %} tags can be indented too. Standalone custom tags are preserved.

Expressions, filters and template tags retain their exact spelling. Inline blocks retain their contents and spacing. HTML tags containing template syntax are preserved in full, including conditional attributes and quoted values containing template tags.

comment, verbatim (including named blocks), blocktrans, blocktranslate, filter and spaceless blocks retain their complete source. This preserves translation messages and whitespace processed by filters. Scripts and styles containing template syntax are also preserved. Ordinary scripts and styles use the existing embedded language formatters. <pre> and <textarea> keep their content.

The HTML options are available under [language.django]. Automatically detected templates use this section. For example, in rainbow.toml,

[language.django]
max_width = 120
indent_size = 4
whitespace_sensitivity = "css"
bracket_same_line = false
void_elements = "no_slash"

whitespace_sensitivity = "strict" preserves template block interiors. HTML comments and {# rainbow: off #} / {# rainbow: on #} comments can carry formatting directives.

Current limits

Template blocks must nest within HTML elements. A conditional that opens an HTML element in one branch and closes it outside that branch is currently reported as unsupported syntax. The CLI leaves the file untouched. Dynamic HTML tag names can also be rejected by the HTML parser.

Detection recognizes shared template delimiters, so Jinja and client-side interpolation can select this pack too. Django is the supported dialect. Jinja-specific block syntax and whitespace controls require separate support.

Verification checks the combined source tree, comments and idempotency. Rendering checks against Django cover representative inline constructs in the test suite. The formatter performs structural parsing without loading a project's template libraries, resolving inheritance or validating custom tag arguments. Django is needed only for the development compatibility tests.