Lint 與格式化 HTML 範本

除了 HTML 範本以外,你技術堆疊裡的每種語言都有格式化工具和 linter。一般的 HTML 工具無法解析 {% %} 和 {{ }},而範本引擎也不在乎周圍的標記長什麼樣子。djLint 會重新格式化充滿範本語法的 HTML、透過 lint 找出常見錯誤,並提供 Django、Jinja、Twig、Nunjucks、Handlebars、Liquid、Go 範本等語言的 profile。

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