跳到內容

Lint 與格式化 HTML 範本

一般的 HTML 工具無法解析 {% %} 和 {{ }},而範本引擎也不在乎周圍的標記長什麼樣子。djLint 會重新格式化充滿範本語法的 HTML、透過 lint 找出常見錯誤,並提供 Django、Jinja、Twig、Nunjucks、Handlebars、Liquid、Go 範本等語言的 profile。它可以在命令列執行,也能作為 pre-commit hook 或編輯器中的語言伺服器。

uv tool install djlint
pip install djlint
npm i -g djlint

messy.html10 個問題沒有問題
{% block content %}<SECTION H009 Tag names should be lowercase. class='posts' H008 Attributes should be double quoted.> H014 Found extra blank lines.<h2>Latest posts</h2>{%if posts%} T001 Variables should be wrapped in a whitespace.<ul>{% for post in posts %}<li><a href="{% url 'post' post.slug %}" class='post-link' H008 Attributes should be double quoted. aria-label="Read {{post.title}}" T001 Variables should be wrapped in a whitespace.>{{post.title}} T001 Variables should be wrapped in a whitespace.</a></li>{% endfor %}</ul>{%else%} T001 Variables should be wrapped in a whitespace.<p>No posts yet.</p>{%endif%} T001 Variables should be wrapped in a whitespace.</SECTION H009 Tag names should be lowercase.>{% endblock %}
{% 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"                           aria-label="Read {{ post.title }}">{{ post.title }}</a>                    </li>                {% endfor %}            </ul>        {% else %}            <p>No posts yet.</p>        {% endif %}    </section>{% endblock %}
{% 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"                            aria-label="Read {{ post.title }}"                        >{{ post.title }}</a>                    </li>                {% endfor %}            </ul>        {% else %}            <p>No posts yet.</p>        {% endif %}    </section>{% endblock %}
djlint messy.html --reformat

為你的範本語言準備的 profile

徽章

看起來像這樣:html style: djLint

為你的專案加上徽章:

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

參與貢獻

歡迎參與貢獻。你可以挑一個 issue 來處理,或開一個包含修正或新功能的 pull request。

GitHub

djLint 由 Christopher Pickering 為 Atlas 專案開發,現在由 monosans 與所有貢獻者維護。