配置
配置写在项目的 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
默认值: false
当给定的路径完全没有匹配到任何文件时,以 0 而不是 2 退出。已找到、但随后被 exclude、extend_exclude、use_gitignore 或 require_pragma 跳过的文件,本来就以 0 退出。
allow_empty_input=trueallow_empty_input=true"allow_empty_input": true--allow-empty-inputblank_line_after_tag
默认值: 无
要在其后添加空行的模板标签,例如 load 和 extends。连续的一组这类标签只在最后一个之后添加空行,块的末尾不会添加空行。
blank_line_after_tag=["load", "extends", "include"]blank_line_after_tag=["load", "extends", "include"]"blank_line_after_tag": ["load", "extends", "include"]--blank-line-after-tag "load,extends,include"blank_line_before_tag
默认值: 无
要在其前添加空行的模板标签,例如 load 和 extends。连续的一组这类标签只在第一个之前添加空行,文件或块的开头不会添加空行。
blank_line_before_tag=["load", "extends", "include"]blank_line_before_tag=["load", "extends", "include"]"blank_line_before_tag": ["load", "extends", "include"]--blank-line-before-tag "load,extends,include"close_void_tags
默认值: false
用斜杠闭合已知的空元素标签,使 <img> 变成 <img />。启用此选项时,不会报告要求相反写法的 H018。
close_void_tags=trueclose_void_tags=true"close_void_tags": true--close-void-tagsconfiguration
全局配置文件,即 pyproject.toml、djlint.toml 或 .djlintrc,例如放在主文件夹中的文件。当两者设置了同一选项时,项目自身的配置文件优先,除非启用了 prefer_configuration。
--configuration ~/.djlintrccustom_blocks
默认值: 无
要缩进的自定义模板块的名称,例如 toc 表示 {% toc %}...{% endtoc %}。
custom_blocks=["toc", "example"]custom_blocks=["toc", "example"]"custom_blocks": ["toc", "example"]--custom-blocks "toc,example"custom_html
默认值: 无
要缩进的自定义 HTML 标签的名称,例如 mjml 或 simple-greeting。名称可以是正则表达式,如 mj-\w+。
custom_html=["mjml", "simple-greeting", 'mj-\w+']custom_html=["mjml", "simple-greeting", 'mj-\w+']"custom_html": ["mjml", "simple-greeting", "mj-\\w+"]--custom-html "mjml,simple-greeting,mj-\w+"exclude
默认值: "__pypackages__,
要跳过的路径,用来替代默认列表。每一项都是正则表达式,与相对于项目根目录或所检查文件夹的路径中完整的文件夹名或文件名匹配,例如 vendor 或 templates/legacy,因此 *.min.html 要写成 .*\.min\.html。
exclude=[".venv", "venv", "build"]exclude=[".venv", "venv", "build"]"exclude": [".venv", "venv", "build"]--exclude ".venv,venv,build"extend_exclude
默认值: 无
要跳过的路径,添加在 exclude 之外;未设置 exclude 时则添加在默认列表之外,和 exclude 一样用正则表达式书写。
extend_exclude=[".custom"]extend_exclude=[".custom"]"extend_exclude": [".custom"]--extend-exclude ".custom"extension
默认值: "html"
在文件夹中查找的文件的扩展名。只能指定一个扩展名;按名称指定的文件无论扩展名是什么都会被检查。
extension="html.dj"extension="html.dj""extension": "html.dj"--extension "html.dj"# or-e "html.dj"files
默认值: 无
要检查的路径,用来替代命令行中的路径,此时命令行中的路径必须是 -,就像使用 stdin 时一样。如果给出了 --stdin-filename,- 无论如何都表示 stdin。
[tool.djlint]files=["index.html"]files=["index.html"]"files": [ "index.html"]index.htmlformat_attribute_js_json
默认值: false
同时格式化 onclick、x-data 等属性中的 JavaScript 和 JSON,即 format_attribute_js_json_pattern 所指定的属性。属性数量少于 format_attribute_js_json_min_props 的对象会保持在一行上。
format_attribute_js_json=trueformat_attribute_js_json=true"format_attribute_js_json": true--format-attribute-js-jsonformat_attribute_js_json_min_props
默认值: 2
只有当对象至少有这么多个属性时才格式化它。属性更少的对象会保持在一行上。
format_attribute_js_json_min_props=3format_attribute_js_json_min_props=3"format_attribute_js_json_min_props": 3--format-attribute-js-json-min-props 3format_attribute_js_json_pattern
默认值: "^(?:on[a-z]+|
用于匹配属性名称的正则表达式,匹配到的属性会作为 JavaScript 或 JSON 格式化。默认值匹配 on* 事件处理器,以及 Alpine、Vue、Angular 和 Aurelia 的绑定。
format_attribute_js_json_pattern="^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"format_attribute_js_json_pattern="^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$""format_attribute_js_json_pattern": "^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"--format-attribute-js-json-pattern "^(on[a-z]+|data-[a-z-]+|x-[a-z-]+)$"format_attribute_template_tags
默认值: false
格式化标签属性中的模板语法。
format_attribute_template_tags=trueformat_attribute_template_tags=true"format_attribute_template_tags": true--format-attribute-template-tagsformat_css
默认值: false
同时使用 css-beautify 格式化 <style> 标签的内容。其中的模板语法尚未得到完全支持。
css 表中的 indent_size 或命令行中的 --indent-css 用于设置 <style> 标签内的缩进,默认为 4。该表也接受其他 css-beautify 选项。
[tool.djlint]format_css=true
[tool.djlint.css]indent_size=5format_css=true
[css]indent_size=5"format_css": true,"css": { "indent_size": 5 }--format-css --indent-css 5format_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=5format_js=true
[js]indent_size=5"format_js": true,"js": { "indent_size": 5 }--format-js --indent-js 5github_output
默认值: 无
以 GitHub Actions 注解的形式输出结果。在 GitHub Actions 中默认开启,可以用 --no-github-output 关闭。请参阅 GitHub Actions。
--github-outputignore
默认值: 无
要跳过的代码检查规则代码。
ignore=["H014", "H015"]ignore=["H014", "H015"]"ignore": ["H014", "H015"]--ignore "H014,H015"ignore_blocks
默认值: 无
不为其内容增加缩进的模板块。其子元素被视为该块的同级元素,并相应地缩进。
ignore_blocks=["raw", "example"]ignore_blocks=["raw", "example"]"ignore_blocks": ["raw", "example"]--ignore-blocks "raw,example"ignore_case
默认值: false
不修正已知 HTML 标签的大小写。启用此选项时,不会报告 H009 和 H010。
[tool.djlint]ignore_case=trueignore_case=true"ignore_case": true--ignore-caseinclude
默认值: 无
要启用的代码检查规则代码,包括默认关闭的规则和预设未包含的规则。
include=["H014", "H015"]include=["H014", "H015"]"include": ["H014", "H015"]--include "H014,H015"indent
默认值: 4
每级缩进的空格数。未设置时,如果 .editorconfig 为该文件指定了 indent_size,djLint 就使用它。
indent=3indent=3"indent": 3--indent 3keep_br_inline
默认值: false
让 <br> 留在它所断开的文本所在的行,而不是单独占一行。<hr> 会渲染为前面内容下方的一条横线,不受影响。
keep_br_inline=truekeep_br_inline=true"keep_br_inline": true--keep-br-inlineline_break_after_multiline_tag
默认值: false
当标签的属性分布在多行时,让其内容另起一行。
line_break_after_multiline_tag=trueline_break_after_multiline_tag=true"line_break_after_multiline_tag": true--line-break-after-multiline-taglinter_output_format
默认值: "{code} {line} {message} {match}"
每条检查消息的格式,由 {filename}、{line}、{end}、{code}、{message} 和 {match} 组成。如果不包含 {filename},输出将按文件分组,并为每组添加标题。
linter_output_format="{filename}:{line}: {code} {message} {match}"linter_output_format="{filename}:{line}: {code} {message} {match}""linter_output_format": "{filename}:{line}: {code} {message} {match}"--linter-output-format "{filename}:{line}: {code} {message} {match}"max_attribute_length
默认值: 70
当标签的属性长度超过此值时,格式化工具会把它们分到多行。
max_attribute_length=10max_attribute_length=10"max_attribute_length": 10--max-attribute-length 10max_blank_lines
默认值: 0
最多保留这么多行连续的空行,因此设为 0 会删除空行。启用 preserve_blank_lines 时此选项无效。
max_blank_lines=5max_blank_lines=5"max_blank_lines": 5--max-blank-lines 5max_line_length
默认值: 120
只有当整行不超过此长度时,格式化工具才会把一些 HTML 和模板标签合并到一行。未设置时,如果 .editorconfig 为该文件指定了 max_line_length,djLint 就使用它。
max_line_length=100max_line_length=100"max_line_length": 100--max-line-length 100name_endblocks
默认值: false
对于跨多行书写的块,把块的名称写入关闭它的 {% endblock %} 中。这正是 T003 所要求的。
name_endblocks=truename_endblocks=true"name_endblocks": true--name-endblocksno_entity_formatting
默认值: false
不把实体引用改写为它所表示的字符,因此 © 会保持原样,而不会变成 ©。承载语法的实体和不可见字符的实体,无论如何都不会被改写。启用此选项时,H023 仍会报告实体;要保留实体,请把 H023 添加到 ignore 中。
no_entity_formatting=trueno_entity_formatting=true"no_entity_formatting": true--no-entity-formattingno_function_formatting
默认值: false
不格式化 {{ }} 中函数调用的参数。
no_function_formatting=trueno_function_formatting=true"no_function_formatting": true--no-function-formattingno_indent_inner_html
默认值: false
不在 <html> 内缩进 <head> 和 <body>,VS Code 内置的 HTML 格式化工具就是这样排版文档的。
no_indent_inner_html=trueno_indent_inner_html=true"no_indent_inner_html": true--no-indent-inner-htmlno_line_after_yaml
默认值: false
不在 YAML front matter 之后添加空行。
no_line_after_yaml=trueno_line_after_yaml=true"no_line_after_yaml": true--no-line-after-yamlno_set_formatting
默认值: false
不格式化 {% set %} 标签的内容。
no_set_formatting=trueno_set_formatting=true"no_set_formatting": true--no-set-formattingper_file_ignores
默认值: 无
在路径与正则表达式匹配的文件中要跳过的代码检查规则代码。在配置文件中,键名是带连字符的 per-file-ignores。
[tool.djlint.per-file-ignores]"file.html"= "H026,H025""file_two.html"="H001"[per-file-ignores]"file.html"= "H026,H025""file_two.html"="H001""per-file-ignores": { "file.html": "H026,H025", "file_two.html":"H001" }--per-file-ignores "file.html" "H026,H025" --per-file-ignores "file_two.html" "H001"prefer_configuration
默认值: false
让 --configuration 指定的文件覆盖项目自身的配置文件。默认情况下,当两者设置了同一选项时,项目自身的配置文件优先。仅限命令行使用,因为它决定的是配置文件本身如何被读取。
--prefer-configurationpreserve_blank_lines
默认值: false
尽可能保留空行。适用于空行有意为之的非 HTML 模板。
preserve_blank_lines=truepreserve_blank_lines=true"preserve_blank_lines": true--preserve-blank-linespreserve_class_newlines
默认值: false
保留跨多行书写的 class 属性中的换行。
preserve_class_newlines=truepreserve_class_newlines=true"preserve_class_newlines": true--preserve-class-newlinespreserve_leading_space
默认值: false
尽可能保留文本行的前导空格。适用于文本缩进有意为之的非 HTML 模板。
preserve_leading_space=truepreserve_leading_space=true"preserve_leading_space": true--preserve-leading-spaceprofile
默认值: 无
模板语言。它决定运行哪些代码检查规则,以及如何格式化模板语法:例如,在 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"profile="django""profile": "django"--profile "django"quiet
默认值: false
不打印差异、进度条和汇总。
quiet=truequiet=true"quiet": true--quietquote_style
默认值: "double"
模板标签内字符串使用的引号,可以是 double 或 single。T002 要求使用相同的引号,格式化工具在 {% if x == "a" %} 这样的条件中也会使用这种引号,这样同一个文件中同一个字符串就不会有两种写法。HTML 属性的引号交由 H008 处理。
quote_style="single"quote_style="single""quote_style": "single"--quote-style singlerequire_pragma
默认值: false
只格式化或检查以 djlint:on 注释开头的文件。该注释可以是 HTML 注释,也可以是 profile 所指定的模板语言中的注释;未设置预设时,可以是任何一种模板语言中的注释。
<!-- djlint:on -->{# djlint:on #}{% comment %} djlint:on {% endcomment %}{{ /* djlint:on */ }}{{!-- djlint:on --}}require_pragma=truerequire_pragma=true"require_pragma": true--require-pragmarules
.djlint_rules.yaml 格式的自定义规则文件,读取它来代替项目根目录下的规则文件。
--rules ../shared/.djlint_rules.yamlsarif
默认值: false
以 SARIF 2.1.0 文档的形式输出检查结果,供 GitHub 代码扫描和其他工具使用。请参阅 GitHub Actions。
--sarifsingle_attribute_per_line
默认值: false
当开始标签需要换行时,把标签名、每个属性和结束尖括号分别放在单独的行上。
single_attribute_per_line=truesingle_attribute_per_line=true"single_attribute_per_line": true--single-attribute-per-linesort_attributes
默认值: false
按名称对属性排序,id 排在第一位,class 排在第二位。如果标签的属性受模板标签控制,则保持原有的书写顺序,因为把属性移出其所在的分支会改变页面。
sort_attributes=truesort_attributes=true"sort_attributes": true--sort-attributessort_load_tags
默认值: false
对 {% load %} 标签中的库进行排序,并删除写了两次的库,因此 {% load static i18n %} 会变成 {% load i18n static %}。在 {% load a b from library %} 中,会对 from 之前的名称排序。如果两个库定义了同名的标签或过滤器,最后加载的那个生效,因此排序可能会改变模板实际使用的是哪一个。
sort_load_tags=truesort_load_tags=true"sort_load_tags": true--sort-load-tagsstatistics
默认值: false
代码检查结束后,输出每个代码被发现的次数。
--statisticsstdin_filename
默认值: "-"
在消息中,以及与 exclude 和 per-file-ignores 匹配时,代表 stdin 的路径。
--stdin-filename templates/index.htmluse_gitignore
默认值: false
同时跳过项目根目录下 .gitignore 中列出的路径。
use_gitignore=trueuse_gitignore=true"use_gitignore": true--use-gitignoreversion
显示版本号并退出。
--versionwarn
默认值: false
存在代码检查错误或需要重新格式化的文件时,以 0 而不是 1 退出。
--warn