入门指南

安装

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 install --upgrade djlint。npm 无法管理或卸载实际安装的包 - 建议尽量直接使用 pip。

npm i djlint

如需在每次提交时运行 djLint,请参阅 pre-commit。

命令行使用方法

djLint 是一个命令行程序。详情请参考配置章节。

Usage: djlint [OPTIONS] SRC ...

  djLint · HTML template linter and formatter.

Options:
  --version                       Show the version and exit.
  -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 不是内部或外部的命令,请确保 Python 在你的环境变量中。

使用路径或标准输入(stdin)

djLint 可以通过路径或标准输入(stdin)运行。

通过路径运行 -

djlint /path/to/templates --lint

或者针对指定文件 -

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

或者使用标准输入(stdin) -

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

标准输入(stdin)也可用于重新格式化代码。输出将仅包含格式化后的代码,而不包含任何消息。

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

输出结果 -

<div></div>

从标准输入(stdin)读取时,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 运行失败。

只有退出码 1 表示 djLint 检查了你的模板并发现了问题。退出码 2 总是表示这次运行本身没有完成,因此流水线可以区别对待这两种情况,而不必猜测。

退出码 2 能发现什么都没检查的运行:路径写错、--extension 不再匹配,或者模板被移动了位置。没有它,这样的运行会悄无声息地通过,而实际上一个模板都没有检查。

djLint 确实找到、但通过 exclude、extend_exclude、use_gitignore 或 require_pragma 有意跳过的文件不算错误。这种运行以 0 退出,因为配置完全按要求生效了。这也让 exclude 能在 pre-commit 下正常工作:无论你是否希望 djLint 检查,pre-commit 都会传入所有已暂存文件的名称。

如果在你的流水线中,路径下没有模板属于正常情况,可以通过 allow_empty_input 关闭退出码 2:

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