Lintowanie i formatowanie szablonów HTML

Każdy język w twoim stosie technologicznym ma formater i linter, poza szablonami HTML. Zwykłe narzędzia do HTML-a nie potrafią przetworzyć {% %} ani {{ }}, a silnikom szablonów jest obojętne, jak wyglądają otaczające je znaczniki. djLint formatuje HTML pełen składni szablonów i wyszukuje w nim typowe błędy. Ma profile dla Django, Jinja, Twig, Nunjucks, Handlebars, Liquid, szablonów Go i innych.

uv tool install djlint
pip install djlint

znajdź swój ulubiony język szablonów!

Przed i po

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

Pochwal się formatowaniem!

Wygląda to tak:

html style: djLint

Dodaj odznakę do swoich projektów:

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

Współtworzenie

Wkład w projekt jest mile widziany. Wyślij pull request z nową funkcją albo przejrzyj listę zgłoszeń i pomóż, gdzie możesz.

GitHub