フォーマッターの使い方

djLint のフォーマッターは、雑に書かれた HTML テンプレートを、一貫性があって読みやすいフォーマットに整えます!

変更を適用する前に、--check で出力を確認してください。

フォーマットで何が変わるかを確認するには、次のコマンドを実行します:

djlint . --check

コードをフォーマットしてファイルを更新するには、次のコマンドを実行します:

djlint . --reformat --single-attribute-per-line

# スクリプトとスタイルもフォーマットするには?
djlint . --reformat --single-attribute-per-line --format-css --format-js

メモ

属性内に書かれた JSON と JavaScript は、--format-attribute-js-json を有効にしない限りそのまま残されます。属性内の HTML は常にそのまま残されます。

メモ

djLint は HTML パーサーでも構文バリデーターでもありません。

例を見てみましょう!

変更前

手入れがどうしても必要な HTML の塊です:

{% load admin_list %}
{% load i18n %}
<p class="paginator">
{% if pagination_required %}
{% for i in page_range %}
{% paginator_number cl i %}
{% endfor %}
{% endif %}
{{ cl.result_count }}
{% if cl.result_count == 1 %}
{{ cl.opts.verbose_name }}
{% else %}
{{ cl.opts.verbose_name_plural }}
{% endif %}
{% if show_all_url %}
<a href="{{ show_all_url }}" class="showall">{% translate 'Show all' %}</a>
{% endif %}
{% if cl.formset and cl.result_count %}
<input type="submit" name="_save" class="default" value="{% translate 'Save' %}">
{% endif %}
</p>

変更後

少し良くなりました……これなら読めますね :)

{% load admin_list %}
{% load i18n %}
<p class="paginator">
    {% if pagination_required %}
        {% for i in page_range %}
            {% paginator_number cl i %}
        {% endfor %}
    {% endif %}
    {{ cl.result_count }}
    {% if cl.result_count == 1 %}
        {{ cl.opts.verbose_name }}
    {% else %}
        {{ cl.opts.verbose_name_plural }}
    {% endif %}
    {% if show_all_url %}
        <a href="{{ show_all_url }}" class="showall">{% translate "Show all" %}</a>
    {% endif %}
    {% if cl.formset and cl.result_count %}
        <input type="submit"
               name="_save"
               class="default"
               value="{% translate 'Save' %}">
    {% endif %}
</p>

single_attribute_per_line を使った変更後

Prettier 形式の属性のほうが好みですか?single_attribute_per_line オプションを有効にしてください:

{% load admin_list %}
{% load i18n %}
<p class="paginator">
    {% if pagination_required %}
        {% for i in page_range %}
            {% paginator_number cl i %}
        {% endfor %}
    {% endif %}
    {{ cl.result_count }}
    {% if cl.result_count == 1 %}
        {{ cl.opts.verbose_name }}
    {% else %}
        {{ cl.opts.verbose_name_plural }}
    {% endif %}
    {% if show_all_url %}
        <a href="{{ show_all_url }}" class="showall">{% translate "Show all" %}</a>
    {% endif %}
    {% if cl.formset and cl.result_count %}
        <input
            type="submit"
            name="_save"
            class="default"
            value="{% translate 'Save' %}"
        >
    {% endif %}
</p>