跳转到内容

配置

配置写在项目的 pyproject.toml、djlint.toml 或 .djlint.toml,或者 .djlintrc 中。命令行选项会覆盖配置文件,但参数只能开启某个选项,因此无法关闭配置文件中开启的开关。项目的设置会覆盖通过 --configuration 指定的全局配置文件,除非设置了 --prefer-configuration。

djLint 从它检查的第一个路径(对 - 而言是 --stdin-filename 所在的文件夹,未指定时是当前目录)开始向上查找这些文件。它会在第一个包含 djlint.toml、.djlint.toml、.djlintrc、.djlint_rules.yaml 或带有 [tool.djlint] 的 pyproject.toml 的文件夹处停下,并且不会越过仓库的根目录(包含 .git 文件夹或文件,或 .hg 文件夹的文件夹)。没有 djLint 设置的 pyproject.toml 不会让它停下,因此 monorepo 中自带 pyproject.toml 的包也会使用仓库的设置。djLint 只读取它停下的文件夹:先读 pyproject.toml 中的 [tool.djlint](其中有设置时),没有则读 djlint.toml 或 .djlint.toml,再没有则读 .djlintrc;同一文件夹中未被读取的文件会给出警告。

命令行和配置都没有设置 indent 或 max_line_length 时,djLint 会像编辑器一样从 .editorconfig 文件中读取 indent_size 或 max_line_length:每个文件按自己的路径取值,最近的 .editorconfig 优先,上层的文件补上缺少的值,直到遇到带有 root = true 的文件或到达项目根目录。

pyproject.toml 的格式为 toml。

[tool.djlint]
profile = "django"
ignore = ["H014", "H015"]

djlint.toml 和 .djlint.toml 的格式为 toml。

profile = "django"
ignore = ["H014", "H015"]

.djlintrc 的格式为 json。

{ "profile": "django", "ignore": ["H014", "H015"] }

像 ignore 这样有多个值的选项接受列表。也可以写成逗号分隔的字符串,例如 "H014,H015",djLint 1.41 之前的版本只认这种写法。

从 1.51 版起,遇到不认识的设置或规则代码时,djLint 会发出警告并给出最接近的已知项。

allow_empty_input

始于 1.44.0 formatter linter --allow-empty-input

默认值: false

当给定的路径完全没有匹配到任何文件时,以 0 而不是 2 退出。已找到、但随后被 exclude、extend_exclude、use_gitignore 或 require_pragma 跳过的文件,本来就以 0 退出。

allow_empty_input=true

blank_line_after_tag

始于 0.4.2 formatter --blank-line-after-tag

默认值: 无

要在其后添加空行的模板标签,例如 load 和 extends。连续的一组这类标签只在最后一个之后添加空行,块的末尾不会添加空行。

blank_line_after_tag=["load", "extends", "include"]

blank_line_before_tag

始于 1.8.0 formatter --blank-line-before-tag

默认值: 无

要在其前添加空行的模板标签,例如 load 和 extends。连续的一组这类标签只在第一个之前添加空行,文件或块的开头不会添加空行。

blank_line_before_tag=["load", "extends", "include"]

close_void_tags

始于 1.26.0 formatter linter --close-void-tags

默认值: false

用斜杠闭合已知的空元素标签,使 <img> 变成 <img />。启用此选项时,不会报告要求相反写法的 H018。

close_void_tags=true

configuration

始于 1.13.0 formatter linter --configuration

全局配置文件,即 pyproject.toml、djlint.toml 或 .djlintrc,例如放在主文件夹中的文件。当两者设置了同一选项时,项目自身的配置文件优先,除非启用了 prefer_configuration。

终端窗口
--configuration ~/.djlintrc

custom_blocks

始于 0.3.5 formatter --custom-blocks

默认值: 无

要缩进的自定义模板块的名称,例如 toc 表示 {% toc %}...{% endtoc %}。

custom_blocks=["toc", "example"]

custom_html

始于 0.7.0 formatter --custom-html

默认值: 无

要缩进的自定义 HTML 标签的名称,例如 mjml 或 simple-greeting。名称可以是正则表达式,如 mj-\w+。

custom_html=["mjml", "simple-greeting", 'mj-\w+']

exclude

始于 0.4.0 linter formatter --exclude

默认值: "__pypackages__,_build,_site,.bzr,.direnv,.eggs,.git,.git-rewrite,.hg,.nox,.svn,.tox,.venv,build,dist,htmlcov,node_modules,site-packages,venv"

要跳过的路径,用来替代默认列表。每一项都是正则表达式,与相对于项目根目录或所检查文件夹的路径中完整的文件夹名或文件名匹配,例如 vendor 或 templates/legacy,因此 *.min.html 要写成 .*\.min\.html。

exclude=[".venv", "venv", "build"]

extend_exclude

始于 0.4.0 linter formatter --extend-exclude

默认值: 无

要跳过的路径,添加在 exclude 之外;未设置 exclude 时则添加在默认列表之外,和 exclude 一样用正则表达式书写。

extend_exclude=[".custom"]

extension

始于 0.0.5 linter formatter --extension

默认值: "html"

在文件夹中查找的文件的扩展名。只能指定一个扩展名;按名称指定的文件无论扩展名是什么都会被检查。

extension="html.dj"

files

始于 1.14.0 linter formatter

默认值: 无

要检查的路径,用来替代命令行中的路径,此时命令行中的路径必须是 -,就像使用 stdin 时一样。如果给出了 --stdin-filename,- 无论如何都表示 stdin。

[tool.djlint]
files=["index.html"]

format_attribute_js_json

始于 1.37.0 formatter --format-attribute-js-json

默认值: false

同时格式化 onclick、x-data 等属性中的 JavaScript 和 JSON,即 format_attribute_js_json_pattern 所指定的属性。属性数量少于 format_attribute_js_json_min_props 的对象会保持在一行上。

format_attribute_js_json=true

format_attribute_js_json_min_props

始于 1.37.0 formatter --format-attribute-js-json-min-props

默认值: 2

只有当对象至少有这么多个属性时才格式化它。属性更少的对象会保持在一行上。

format_attribute_js_json_min_props=3

format_attribute_js_json_pattern

始于 1.37.0 formatter --format-attribute-js-json-pattern

默认值: "^(?:on[a-z]+|x-[a-z\\-]+|@[a-z\\-]+|:[a-z\\-]+|v-[a-z\\-]+|\\([a-z\\-]+\\)|\\[[a-z\\-]+\\]|\\*ng[A-Z][a-zA-Z]*|[a-z\\-]+\\.(bind|delegate|call|trigger))$"

用于匹配属性名称的正则表达式,匹配到的属性会作为 JavaScript 或 JSON 格式化。默认值匹配 on* 事件处理器,以及 Alpine、Vue、Angular 和 Aurelia 的绑定。

format_attribute_js_json_pattern="^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"

format_attribute_template_tags

始于 0.6.7 formatter --format-attribute-template-tags

默认值: false

格式化标签属性中的模板语法。

format_attribute_template_tags=true

format_css

始于 1.9.0 formatter --format-css

默认值: false

同时使用 css-beautify 格式化 <style> 标签的内容。其中的模板语法尚未得到完全支持。

css 表中的 indent_size 或命令行中的 --indent-css 用于设置 <style> 标签内的缩进,默认为 4。该表也接受其他 css-beautify 选项。

[tool.djlint]
format_css=true
[tool.djlint.css]
indent_size=5

format_js

始于 1.9.0 formatter --format-js

默认值: false

同时使用 js-beautify 格式化 <script> 标签中的 JavaScript 和 JSON。type 既不是 JavaScript 也不是 JSON 的脚本(例如 text/template 中的客户端模板)保持原样。其中的模板语法尚未得到完全支持。

js 表中的 indent_size 或命令行中的 --indent-js 用于设置 <script> 标签内的缩进,默认为 4。该表也接受其他 js-beautify 选项。

[tool.djlint]
format_js=true
[tool.djlint.js]
indent_size=5

github_output

始于 1.37.0 formatter linter --github-output

默认值: 无

以 GitHub Actions 注解的形式输出结果。在 GitHub Actions 中默认开启,可以用 --no-github-output 关闭。请参阅 GitHub Actions。

终端窗口
--github-output

ignore

始于 0.1.5 linter --ignore

默认值: 无

要跳过的代码检查规则代码。

ignore=["H014", "H015"]

ignore_blocks

始于 1.24.0 formatter --ignore-blocks

默认值: 无

不为其内容增加缩进的模板块。其子元素被视为该块的同级元素,并相应地缩进。

ignore_blocks=["raw", "example"]

ignore_case

始于 1.23.0 formatter linter --ignore-case

默认值: false

不修正已知 HTML 标签的大小写。启用此选项时,不会报告 H009 和 H010。

[tool.djlint]
ignore_case=true

include

始于 1.20.0 linter --include

默认值: 无

要启用的代码检查规则代码,包括默认关闭的规则和预设未包含的规则。

include=["H014", "H015"]

indent

始于 0.3.5 formatter --indent

默认值: 4

每级缩进的空格数。未设置时,如果 .editorconfig 为该文件指定了 indent_size,djLint 就使用它。

indent=3

keep_br_inline

始于 1.45.0 formatter --keep-br-inline

默认值: false

让 <br> 留在它所断开的文本所在的行,而不是单独占一行。<hr> 会渲染为前面内容下方的一条横线,不受影响。

keep_br_inline=true

line_break_after_multiline_tag

始于 1.27.0 formatter --line-break-after-multiline-tag

默认值: false

当标签的属性分布在多行时,让其内容另起一行。

line_break_after_multiline_tag=true

linter_output_format

始于 0.6.7 linter --linter-output-format

默认值: "{code} {line} {message} {match}"

每条检查消息的格式,由 {filename}、{line}、{end}、{code}、{message} 和 {match} 组成。如果不包含 {filename},输出将按文件分组,并为每组添加标题。

linter_output_format="{filename}:{line}: {code} {message} {match}"

max_attribute_length

始于 0.5.8 formatter --max-attribute-length

默认值: 70

当标签的属性长度超过此值时,格式化工具会把它们分到多行。

max_attribute_length=10

max_blank_lines

始于 1.31.0 formatter --max-blank-lines

默认值: 0

最多保留这么多行连续的空行,因此设为 0 会删除空行。启用 preserve_blank_lines 时此选项无效。

max_blank_lines=5

max_line_length

始于 0.5.7 formatter --max-line-length

默认值: 120

只有当整行不超过此长度时,格式化工具才会把一些 HTML 和模板标签合并到一行。未设置时,如果 .editorconfig 为该文件指定了 max_line_length,djLint 就使用它。

max_line_length=100

name_endblocks

始于 1.45.0 formatter --name-endblocks

默认值: false

对于跨多行书写的块,把块的名称写入关闭它的 {% endblock %} 中。这正是 T003 所要求的。

name_endblocks=true

no_entity_formatting

始于 1.45.0 formatter --no-entity-formatting

默认值: false

不把实体引用改写为它所表示的字符,因此 &copy; 会保持原样,而不会变成 ©。承载语法的实体和不可见字符的实体,无论如何都不会被改写。启用此选项时,H023 仍会报告实体;要保留实体,请把 H023 添加到 ignore 中。

no_entity_formatting=true

no_function_formatting

始于 1.30.2 formatter --no-function-formatting

默认值: false

不格式化 {{ }} 中函数调用的参数。

no_function_formatting=true

no_indent_inner_html

始于 1.45.0 formatter --no-indent-inner-html

默认值: false

不在 <html> 内缩进 <head> 和 <body>,VS Code 内置的 HTML 格式化工具就是这样排版文档的。

no_indent_inner_html=true

no_line_after_yaml

始于 1.29.0 formatter --no-line-after-yaml

默认值: false

不在 YAML front matter 之后添加空行。

no_line_after_yaml=true

no_set_formatting

始于 1.30.2 formatter --no-set-formatting

默认值: false

不格式化 {% set %} 标签的内容。

no_set_formatting=true

per_file_ignores

始于 1.7.0 linter --per-file-ignores

默认值: 无

在路径与正则表达式匹配的文件中要跳过的代码检查规则代码。在配置文件中,键名是带连字符的 per-file-ignores。

[tool.djlint.per-file-ignores]
"file.html"= "H026,H025"
"file_two.html"="H001"

prefer_configuration

始于 1.45.0 formatter linter --prefer-configuration

默认值: false

让 --configuration 指定的文件覆盖项目自身的配置文件。默认情况下,当两者设置了同一选项时,项目自身的配置文件优先。仅限命令行使用,因为它决定的是配置文件本身如何被读取。

终端窗口
--prefer-configuration

preserve_blank_lines

始于 1.3.0 formatter --preserve-blank-lines

默认值: false

尽可能保留空行。适用于空行有意为之的非 HTML 模板。

preserve_blank_lines=true

preserve_class_newlines

始于 1.39.0 formatter --preserve-class-newlines

默认值: false

保留跨多行书写的 class 属性中的换行。

preserve_class_newlines=true

preserve_leading_space

始于 1.2.0 formatter --preserve-leading-space

默认值: false

尽可能保留文本行的前导空格。适用于文本缩进有意为之的非 HTML 模板。

preserve_leading_space=true

profile

始于 0.4.5 linter formatter --profile

默认值: 无

模板语言。它决定运行哪些代码检查规则,以及如何格式化模板语法:例如,在 handlebars 中,{{#if}} 标签内部没有空格。

未设置预设时,djLint 会像 all 一样格式化所有模板语言的语法,并使用 html 的规则进行代码检查。

可选值:

  • html
  • django
  • jinja
  • nunjucks(用于 nunjucks 和 twig)
  • handlebars(用于 handlebars 和 mustache)
  • liquid(shopify、jekyll、eleventy)
  • golang(go 模板;hugo、helm)
  • angular
  • tera(也用于 zola;minijinja 请使用 jinja)
  • askama(rust 中的 jinja 风格模板;rust 表达式永远不会被重新格式化)
  • mako(控制行和 ## 注释保持原样)
  • all(同时适用于所有模板语言)
profile="django"

quiet

始于 0.0.9 formatter linter --quiet

默认值: false

不打印差异、进度条和汇总。

quiet=true

quote_style

始于 1.45.0 formatter linter --quote-style

默认值: "double"

模板标签内字符串使用的引号,可以是 double 或 single。T002 要求使用相同的引号,格式化工具在 {% if x == "a" %} 这样的条件中也会使用这种引号,这样同一个文件中同一个字符串就不会有两种写法。HTML 属性的引号交由 H008 处理。

quote_style="single"

require_pragma

始于 0.5.8 formatter linter --require-pragma

默认值: false

只格式化或检查以 djlint:on 注释开头的文件。该注释可以是 HTML 注释,也可以是 profile 所指定的模板语言中的注释;未设置预设时,可以是任何一种模板语言中的注释。

<!-- djlint:on -->
{# djlint:on #}
{% comment %} djlint:on {% endcomment %}
{{ /* djlint:on */ }}
{{!-- djlint:on --}}
require_pragma=true

rules

始于 1.41.0 formatter linter --rules

.djlint_rules.yaml 格式的自定义规则文件,读取它来代替项目根目录下的规则文件。

终端窗口
--rules ../shared/.djlint_rules.yaml

sarif

始于 1.46.0 formatter linter --sarif

默认值: false

以 SARIF 2.1.0 文档的形式输出检查结果,供 GitHub 代码扫描和其他工具使用。请参阅 GitHub Actions。

终端窗口
--sarif

single_attribute_per_line

始于 1.40.0 formatter --single-attribute-per-line

默认值: false

当开始标签需要换行时,把标签名、每个属性和结束尖括号分别放在单独的行上。

single_attribute_per_line=true

sort_attributes

始于 1.45.0 formatter --sort-attributes

默认值: false

按名称对属性排序,id 排在第一位,class 排在第二位。如果标签的属性受模板标签控制,则保持原有的书写顺序,因为把属性移出其所在的分支会改变页面。

sort_attributes=true

sort_load_tags

始于 1.47.0 formatter --sort-load-tags

默认值: false

对 {% load %} 标签中的库进行排序,并删除写了两次的库,因此 {% load static i18n %} 会变成 {% load i18n static %}。在 {% load a b from library %} 中,会对 from 之前的名称排序。如果两个库定义了同名的标签或过滤器,最后加载的那个生效,因此排序可能会改变模板实际使用的是哪一个。

sort_load_tags=true

statistics

始于 1.16.0 linter --statistics

默认值: false

代码检查结束后,输出每个代码被发现的次数。

终端窗口
--statistics

stdin_filename

始于 1.43.0 formatter linter --stdin-filename

默认值: "-"

在消息中,以及与 exclude 和 per-file-ignores 匹配时,代表 stdin 的路径。

终端窗口
--stdin-filename templates/index.html

use_gitignore

始于 0.5.9 linter formatter --use-gitignore

默认值: false

同时跳过项目根目录下 .gitignore 中列出的路径。

use_gitignore=true

version

始于 0.3.9 formatter linter --version

显示版本号并退出。

终端窗口
--version

warn

始于 0.7.6 formatter linter --warn

默认值: false

存在代码检查错误或需要重新格式化的文件时,以 0 而不是 1 退出。

终端窗口
--warn