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