检查并格式化 HTML 模板

你的技术栈里,除了 HTML 模板,每种语言都有自己的格式化工具和代码检查工具。通用的 HTML 工具无法解析 {% %} 和 {{ }},模板引擎又不关心周围的标记写成什么样。djLint 能重新格式化大量使用模板语法的 HTML,并检查其中的常见错误,还为 Django、Jinja、Twig、Nunjucks、Handlebars、Liquid、Go 模板等提供了预设。它可以在命令行中运行,也可以作为 pre-commit 钩子或编辑器中的语言服务器运行。

uv tool install djlint
pip install djlint

找到你喜爱的模板语言!

前后对比

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 %}

亮出你的格式!

效果如下:

html style: djLint

为你的项目添加徽章:

[![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

参与贡献

欢迎参与贡献。你可以提交实现新功能的 PR,或者查看 issue 列表,在力所能及的地方提供帮助。

GitHub