跳转到内容

快速入门

djLint 发布在 PyPI 和 npm 上。PyPI 版需要 Python 3.11 或更高版本,npm 版自带 Python。在 Python 项目中用 uv 或 pip 添加,在 Angular 应用、Eleventy 网站或 Shopify 主题等 JavaScript 项目中用 npm 添加。

使用 uv 时,可以将其添加为项目的开发依赖:

终端窗口
uv add --dev djlint

或者将其安装为独立工具:

终端窗口
uv tool install djlint

要在编辑器中或每次提交时运行 djLint,请参阅集成和 pre-commit。

把模板所在的文件夹交给 djLint,并用 --profile 指定模板语言。启用哪些检查规则、模板语法如何格式化,都由预设决定。以 Django 项目为例:

检查模板(检查是默认模式,所以可以省略 --lint):

终端窗口
djlint templates --lint --profile=django

查看格式化会改动什么。使用 --check 时,djLint 只输出 diff,不会修改文件:

终端窗口
djlint templates --check --profile=django

就地格式化文件:

终端窗口
djlint templates --reformat --profile=django

所有预设都列在 profile 选项中。每种模板语言也有自己的页面,例如 Django、Jinja 或 Handlebars,其中还介绍了如何在配置文件中设置预设,免得每次都要传入。

如果是用 uv add --dev 把 djLint 加进项目的,它位于项目的环境中:在命令前加上 uv run,例如 uv run djlint templates --lint --profile=django。 如果是用 npm i -D 添加的,则改为在前面加上 npx,例如 npx djlint templates --lint --profile=django。

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 没有检查你要求它检查的内容:路径没有匹配到任何文件、某个文件无法读取(不是 UTF-8,或没有权限)、命令行或配置无效,或者 djLint 运行失败。

只有代码 1 表示 djLint 检查了你的模板,并且对发现的内容不满意,因此流水线可以把检查未通过与什么都没检查的运行区分开来,例如路径写错了,或者 --extension 不再匹配。

djLint 找到、但随后因 exclude、extend_exclude、use_gitignore 或 require_pragma 而有意跳过的文件,不算错误,也正因如此,exclude 才能在 pre-commit 下正常工作。如果某个路径没有模板在你的流水线中是正常情况,可以用 allow_empty_input 关闭代码 2:

终端窗口
djlint /path/to/templates --lint --allow-empty-input
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 Regexes of the paths to skip, comma-separated,
in place of the default list.
--extend-exclude PATHS Regexes of the paths to skip on top of the
exclude list, comma-separated.
--use-gitignore Also skip the paths that the .gitignore in the
project root 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 the linter runs the rules of 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
&copy; 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.