Issue with linters reformatting single-line block tags in Django templates

Hi everyone,

I’m running into a problem with how linters and formatters like djLint handle block tags in Django templates. I often use blocks for simple strings, such as class names, and prefer keeping them on a single line to avoid unnecessary whitespace in the output. For example:

{% block card_column_class %}col-lg-6 mx-auto{% endblock card_column_class %}

When I run djLint, it reformats this into:

{% block card_column_class %}
    col-lg-6 mx-auto
{% endblock card_column_class %}

This adds newlines and indentation, introducing extra whitespace in the rendered HTML. Although browsers typically handle extra spaces in class attributes, I’d prefer to keep the output clean and concise.

For blocks with multiple lines of HTML or complex content, the multi-line formatting makes sense, but for simple cases like this, it feels excessive. Right now, I have to disable the linter or formatter for those lines, which isn’t ideal as it disrupts the workflow.

Has anyone else dealt with this? Is there a recommended approach in Django for managing this?

I’d love to hear your thoughts, workarounds, or suggestions. Thanks in advance!

I am not 100% sure, but you could look into the max_attribute_length config option of djLint.

As per their documentation:

Formatter will attempt to wrap tag attributes if the attribute length exceeds this value.

The problem with opinionated formatters is… they have opinions.

Could you use something like django-minify-html to post-process the rendered html?

Or you could maybe create your own custom “inline block” template tag that djLint wouldn’t format as a block:

{% inline_block card_column_class col-lg-6 mx-auto %}

That’d be a custom tag that parses to a BlockNode (like the standard block tag), using its arguments to build the content nodelist. (Someone who knows more about the template engine than I do might have a better approach.)