Lint 與格式化 HTML 範本
除了 HTML 範本以外,你技術堆疊裡的每種語言都有格式化工具和 linter。一般的 HTML 工具無法解析 {% %} 和 {{ }},而範本引擎也不在乎周圍的標記長什麼樣子。djLint 會重新格式化充滿範本語法的 HTML、透過 lint 找出常見錯誤,並提供 Django、Jinja、Twig、Nunjucks、Handlebars、Liquid、Go 範本等語言的 profile。
uv tool install djlint
pip install djlint
{% 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 %}
{% 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 %}
看起來像這樣:
為你的專案加上徽章:
[](https://github.com/djlint/djlint)
.. image:: https://img.shields.io/badge/html%20style-djLint-blue.svg
:target: https://github.com/djlint/djlint