快速入门
安装
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 替换较新的 djLint。npm 不会管理或卸载实际安装的包,因此请尽量使用 pip。
npm i djlint
要在每次提交时运行 djLint,请参阅 pre-commit。
命令行用法
djLint 是一个命令行应用程序。高级配置请参阅配置页面。
Usage: djlint [OPTIONS] SRC ...
djLint · HTML template linter and formatter.
Modes:
--lint Lint the files. This is the default when
neither --reformat nor --check is given.
--reformat Format the files in place.
--check Show what formatting would change, without
changing the files.
--lsp Run as a language server over stdio, for
editors.
Files:
-e, --extension EXT Extension of the files to look for in folders.
[default: html]
--exclude PATHS Paths to skip, comma-separated, in place of
the default list.
--extend-exclude PATHS Paths to skip on top of the default list,
comma-separated.
--use-gitignore Also skip the paths that .gitignore lists.
--require-pragma Only format or lint files whose first line is
a djlint:on comment.
--stdin-filename PATH Path that stands for stdin in messages,
excludes and per-file ignores. [default: -]
--allow-empty-input Exit with 0 instead of 2 when the given paths
match no files.
Configuration:
--profile NAME Template language: html, django, jinja,
nunjucks, handlebars, liquid, golang, angular,
tera, askama, mako or all. It decides which
linter rules run and how template syntax is
formatted. Without one, formatting follows all
and linting follows html.
--configuration FILE Global config file: a pyproject.toml,
djlint.toml or .djlintrc. The project's own
config file wins where both set an option.
--prefer-configuration Let the --configuration file win over the
project's own config file.
Linter:
-i, --ignore CODES Linter codes to skip, comma-separated, for
example H014,H017.
--include CODES Linter codes to turn on, comma-separated, for
example H017. Works for rules that are off by
default and for rules the profile leaves out.
--per-file-ignores <PATTERN CODES>...
Skip linter codes in the files whose path
matches a regex. Can be repeated.
--rules FILE Custom linter rules, in the .djlint_rules.yaml
format.
Output:
--quiet Do not print diffs, the progress bar or the
summary.
--warn Exit with 0 instead of 1 when there are lint
errors or files to reformat.
--statistics After linting, print how many times each code
was found.
--linter-output-format FORMAT Layout of each lint message, from {filename},
{line}, {end}, {code}, {message} and {match}.
[default: {code} {line} {message} {match}]
--github-output / --no-github-output
Print results as GitHub Actions annotations.
On by default inside GitHub Actions.
--sarif Print findings as a SARIF 2.1.0 JSON document,
for GitHub code scanning and other tools.
Layout:
--indent N Spaces per indent level. [default: 4, or
indent_size from .editorconfig]
--max-line-length N Join tags onto one line only when the line
fits within this length. [default: 120, or
max_line_length from .editorconfig]
--max-attribute-length N Spread a tag's attributes over lines once they
are longer than this. [default: 70]
--single-attribute-per-line When an opening tag wraps, put each attribute
on its own line.
--line-break-after-multiline-tag
When a tag's attributes are spread over lines,
start its content on a new line.
--no-indent-inner-html Do not indent <head> and <body> inside <html>.
--keep-br-inline Keep <br> on the line of the text it breaks.
--custom-blocks TAGS Names of the custom template blocks to indent,
comma-separated, for example toc.
--custom-html TAGS Names of the custom HTML tags to indent,
comma-separated, for example mjml.
--ignore-blocks TAGS Template blocks that do not indent their
contents, comma-separated.
--blank-line-after-tag TAGS Template tags to follow with a blank line,
comma-separated, for example load,extends.
--blank-line-before-tag TAGS Template tags to precede with a blank line,
comma-separated, for example load,extends.
--max-blank-lines N Keep at most this many blank lines in a row.
Has no effect with --preserve-blank-lines.
[default: 0]
--preserve-blank-lines Keep blank lines where possible.
--preserve-leading-space Keep the leading spaces of text lines where
possible.
--no-line-after-yaml Do not add a blank line after YAML front
matter.
Rewrites:
--close-void-tags Close void tags with a slash, so <img> becomes
<img />.
--ignore-case Do not fix the case of known HTML tags.
--sort-attributes Sort attributes by name, with id first and
class second.
--sort-load-tags Sort the libraries in {% load %} tags and drop
repeated ones.
--name-endblocks Name the block in its {% endblock %} when the
block spans lines.
--quote-style [double|single] Quotes for strings inside template tags.
[default: double]
--no-entity-formatting Do not rewrite entity references such as
© as the characters they name.
--no-function-formatting Do not format the arguments of function calls
inside {{ }}.
--no-set-formatting Do not format the contents of {% set %} tags.
--preserve-class-newlines Keep the line breaks in class attributes
written across lines.
--format-attribute-template-tags
Format template syntax inside tag attributes.
CSS and JavaScript:
--format-css Also format the contents of <style> tags, with
css-beautify.
--indent-css N Indent width inside <style> tags, for
--format-css. [default: 4]
--format-js Also format JavaScript and JSON in <script>
tags, with js-beautify.
--indent-js N Indent width inside <script> tags, for
--format-js. [default: 4]
--format-attribute-js-json Also format JavaScript and JSON in attributes
such as onclick and x-data.
--format-attribute-js-json-pattern REGEX
Regex for the names of the attributes to
format as JavaScript or JSON. [default: on*
event handlers and Alpine, Vue, Angular and
Aurelia bindings]
--format-attribute-js-json-min-props N
Format an object only when it has at least
this many properties. [default: 2]
Other:
--version Show the version and exit.
-h, --help Show this message and exit.
使用路径还是标准输入
djLint 既可以处理路径,也可以处理标准输入(stdin)。
指定路径运行:
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 运行失败。 |
只有代码 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