Lint e formatação de templates HTML

Toda linguagem da sua stack tem um formatador e um linter, menos os templates HTML. Ferramentas genéricas de HTML não conseguem interpretar {% %} e {{ }}, e os motores de templates não se importam com a aparência da marcação ao redor. O djLint reformata HTML repleto de sintaxe de template e faz o lint em busca de erros comuns, com perfis para Django, Jinja, Twig, Nunjucks, Handlebars, Liquid, templates Go e muito mais.

uv tool install djlint
pip install djlint

encontre a sua linguagem de template favorita!

Antes e depois

messy.html
{% block content %}
<SECTION class="posts">


<h2>Latest posts</h2>
{%if posts%}
<ul>
{% for post in posts %}
<li>
<a href="{% url 'post' post.slug %}" class="post-link {%if post.featured%}is-featured{%endif%}" data-analytics-id="post-{{post.id}}" aria-label="Read {{post.title}}">{{post.title|title}}</a>
</li>
{% endfor %}
</ul>
{%else%}
<p>No posts yet.</p>
{%endif%}
</SECTION>
{% endblock %}
$ djlint messy.html --reformat --single-attribute-per-line
{% block content %}
    <section class="posts">
        <h2>Latest posts</h2>
        {% if posts %}
            <ul>
                {% for post in posts %}
                    <li>
                        <a
                            href="{% url 'post' post.slug %}"
                            class="post-link {% if post.featured %}is-featured{% endif %}"
                            data-analytics-id="post-{{ post.id }}"
                            aria-label="Read {{ post.title }}"
                        >{{ post.title|title }}</a>
                    </li>
                {% endfor %}
            </ul>
        {% else %}
            <p>No posts yet.</p>
        {% endif %}
    </section>
{% endblock %}

Mostre a sua formatação!

Fica assim:

html style: djLint

Adicione um selo aos seus projetos:

[![Code style: djLint](https://img.shields.io/badge/html%20style-djLint-blue.svg)](https://github.com/djlint/djlint)
.. image:: https://img.shields.io/badge/html%20style-djLint-blue.svg
   :target: https://github.com/djlint/djlint

Como contribuir

Contribuições são bem-vindas. Envie um PR com um novo recurso, ou confira a lista de issues e ajude no que puder.

GitHub