はじめに

インストール

djLint は Python で作られており、PyPI で公開されています。uv を使う場合は、開発用の依存関係としてプロジェクトに追加します:

uv add --dev djlint

または、単独のツールとしてインストールします:

uv tool install djlint

または、pip でインストールします:

pip install djlint

または、pipx でインストールします:

pipx install djlint

または、macOS や Linux では、コミュニティが管理している Homebrew formula でインストールします:

brew install djlint

または、npm でインストールします。警告:npm パッケージはラッパーにすぎません。そのインストールスクリプトはシステムのパス上にある python3 で pip を実行し、その npm リリースに対応するバージョンの djLint をインストールするため、新しい djLint が古いものに置き換えられることがあります。npm は実際のパッケージを管理もアンインストールもしないため、できれば pip を使ってください。

npm i djlint

コミットのたびに djLint を実行するには、pre-commit を参照してください。

CLI の使い方

djLint はコマンドラインアプリケーションです。高度な設定については設定のページを参照してください。

Usage: djlint [OPTIONS] SRC ...

  djLint · HTML template linter and formatter.

Options:
  --version                       Show the version and exit.
  --lsp                           Run as a language server over stdio, for
                                  editors.
  -e, --extension TEXT            File extension to check [default: html]
  -i, --ignore TEXT               Codes to ignore. ex: "H014,H017"
  --reformat                      Reformat the file(s).
  --check                         Check formatting on the file(s).
  --stdin-filename TEXT           Filename to use for per-file-ignores and
                                  messages when reading from stdin. [default: -]
  --indent INTEGER                Indent spacing. [default: 4]
  --quiet                         Do not print diff when reformatting.
  --profile TEXT                  Enable defaults by template language. ops:
                                  django, jinja, nunjucks, handlebars, liquid,
                                  golang, angular, tera, askama, html [default:
                                  html]
  --require-pragma                Only format or lint files that starts with a
                                  comment with the text 'djlint:on'
  --lint                          Lint for common issues. [default option]
  --use-gitignore                 Use .gitignore file to extend excludes.
  --allow-empty-input             Exit with 0 instead of 2 when the given paths
                                  match no files.
  --warn                          Return errors as warnings.
  --preserve-leading-space        Attempt to preserve leading space on text.
  --preserve-blank-lines          Attempt to preserve blank lines.
  --preserve-class-newlines       Preserve line breaks inside multiline class
                                  attributes.
  --format-css                    Also format contents of <style> tags.
  --format-js                     Also format contents of <script> tags.
  --format-attribute-js-json      Also format JavaScript/JSON inside HTML
                                  attributes.
  --format-attribute-js-json-pattern TEXT
                                  Regex pattern to match JavaScript attributes.
  --format-attribute-js-json-min-props INTEGER
                                  Minimum number of properties to treat
                                  attribute content as JS/JSON.
  --configuration FILE            Path to global configuration file in
                                  djlint.toml, .djlint.toml, or .djlintrc format
  --prefer-configuration          Let --configuration override the project's own
                                  config file, rather than the other way round.
  --rules FILE                    Path to custom rules file in
                                  .djlint_rules.yaml format
  --statistics                    Count the number of occurrences of each
                                  error/warning code.
  --include TEXT                  Codes to include. ex: "H014,H017"
  --ignore-case                   Do not fix case on known html tags.
  --ignore-blocks TEXT            Comma list of template blocks to not indent.
  --blank-line-after-tag TEXT     Add an additional blank line after {% <tag>
                                  ... %} tag groups.
  --blank-line-before-tag TEXT    Add an additional blank line before {% <tag>
                                  ... %} tag groups.
  --line-break-after-multiline-tag
                                  Do not condense the content of multi-line tags
                                  into the line of the last attribute.
  --custom-blocks TEXT            Indent custom template blocks. For example {%
                                  toc %}...{% endtoc %}
  --custom-html TEXT              Indent custom HTML tags. For example <mjml>
  --exclude TEXT                  Override the default exclude paths.
  --extend-exclude TEXT           Add additional paths to the default exclude.
  --linter-output-format TEXT     Customize order of linter output message.
  --max-line-length INTEGER       Max line length. [default: 120]
  --max-attribute-length INTEGER  Max attribute length. [default: 70]
  --format-attribute-template-tags
                                  Attempt to format template syntax inside of
                                  tag attributes.
  --single-attribute-per-line     When an opening tag wraps, put each attribute
                                  on its own line.
  --per-file-ignores <TEXT TEXT>...
                                  Ignore linter rules on a per-file basis.
  --indent-css INTEGER            Set CSS indent level.
  --indent-js INTEGER             Set JS indent level.
  --close-void-tags               Add closing mark on known void tags. Ex: <img>
                                  becomes <img />
  --no-line-after-yaml            Do not add a blank line after yaml front
                                  matter.
  --name-endblocks                Name the endblock of a block written across
                                  lines.
  --sort-attributes               Sort attributes by name, with id first and
                                  class second.
  --sort-load-tags                Sort the libraries in {% load %} tags.
  --no-indent-inner-html          Do not indent <head> and <body> below <html>.
  --no-function-formatting        Do not attempt to format function contents.
  --no-set-formatting             Do not attempt to format set contents.
  --keep-br-inline                Keep <br> on the line of the text it breaks.
  --no-entity-formatting          Do not rewrite entity references as
                                  characters.
  --quote-style [double|single]   Quotes to use for strings inside template
                                  tags. [default: double]
  --max-blank-lines INTEGER       Consolidate blank lines down to x lines.
                                  [default: 0]
  --github-output / --no-github-output
                                  Output GitHub-compatible formatting.
  --sarif                         Output findings as SARIF 2.1.0 json, for
                                  GitHub code scanning and other tools.
  -h, --help                      Show this message and exit.

パスと標準入力の使い分け

djLint はパスと標準入力のどちらでも動作します。

パスを指定して実行する場合:

djlint /path/to/templates --lint

特定のファイルを指定する場合:

djlint /path/to/this.mustache --lint

標準入力を使う場合:

echo "<div></div>" | djlint -

標準入力はコードの再フォーマットにも使えます。出力はフォーマット済みのコードだけで、メッセージは含まれません。

echo "<div></div>" | djlint - --reformat

出力:

<div></div>

標準入力から読み込む場合、djLint には per-file-ignores との照合やメッセージの表示に使う実際のパスがないため、デフォルトで - を使います。エディターとの連携機能が保存時にファイルの内容をパイプで渡す場合などは、--stdin-filename を指定して、パイプで渡した内容の実際のパスを djLint に伝えてください。

echo "<div></div>" | djlint - --stdin-filename templates/index.html

終了コード

コード 意味
0 djLint にチェックを指示したものすべてに問題がないか、見つかったファイルがすべて設定によってスキップされました。
1 djLint がリントエラー、または再フォーマットが必要なファイルを見つけました。--warn を指定すると、これらを警告として報告し、代わりに 0 で終了します。
2 djLint は指示したものをチェックしませんでした。パスに一致するファイルがなかったか、コマンドラインや設定が無効だったか、djLint が失敗しました。

djLint がテンプレートを調べて問題を見つけたことを意味するのは、コード 1 だけです。コード 2 は常に、実行そのものが役目を果たさなかったことを意味するため、パイプラインはこの 2 つを区別して扱えます。

まったく何もチェックしなかった実行は、コード 2 で検出できます。パスの誤り、もう一致しなくなった --extension、移動したテンプレートなどがこれにあたります。コード 2 がなければ、このような実行は黙って成功してしまいます。

djLint が 実際に 見つけたうえで、exclude、extend_exclude、use_gitignore、require_pragma によって意図的にスキップしたファイルはエラーではありません。この場合の実行は 0 で終了します。設定が指示どおりに動作したからです。pre-commit は、djLint に見てほしいかどうかに関係なくステージされたファイルの名前を渡しますが、そこで exclude が機能するのもこのためです。

テンプレートがないパスがパイプラインにとって正常な場合は、allow_empty_input でコード 2 を無効にしてください:

djlint /path/to/templates --lint --allow-empty-input