開始使用
djLint 發布在 PyPI 和 npm 上。PyPI 版需要 Python 3.11 或更新版本,npm 版則自帶 Python。在 Python 專案中用 uv 或 pip 加入,在 Angular 應用程式、Eleventy 網站或 Shopify 佈景主題等 JavaScript 專案中用 npm 加入。
pip install djlintnpm 套件自帶 Python,所以只需要 Node.js 16.9 或更新版本。它提供 Linux(x64、Arm64、ppc64le、s390x、riscv64)、macOS(x64、Arm64)和 Windows(x64、x86、Arm64)的建置。在使用 musl 的 Linux(如 Alpine)上,它支援 x64 與 Arm64。將它加入專案作為開發相依套件:
npm i -D djlint或者全域安裝:
npm i -g djlint使用 pipx:
pipx install djlint在 macOS 或 Linux 上使用由社群維護的 Homebrew formula:
brew install djlint若要在編輯器中或每次提交時執行 djLint,請參閱整合和 pre-commit。
把範本所在的資料夾交給 djLint,並用 --profile 指定範本語言。要執行哪些 linter 規則、範本語法要怎麼格式化,都由 profile 決定。以 Django 專案為例:
lint 範本(lint 是預設模式,所以 --lint 可以省略):
djlint templates --lint --profile=django查看格式化會改動哪些地方。加上 --check 時,djLint 只會輸出 diff,不會修改檔案:
djlint templates --check --profile=django直接在原處格式化檔案:
djlint templates --reformat --profile=django所有 profile 都列在 profile 選項中。每種範本語言也有自己的頁面,例如 Django、Jinja 或 Handlebars,頁面上也說明了如何在設定檔中設定 profile,省得每次都要傳入。
如果是用 uv add --dev 把 djLint 加入專案,它位於專案的環境中:請在指令前加上 uv run,例如 uv run djlint templates --lint --profile=django。 如果是用 npm i -D 加入的,請改在前面加上 npx,例如 npx djlint templates --lint --profile=django。
使用路徑與使用 stdin
Section titled “使用路徑與使用 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 發現了 lint 錯誤,或有需要重新格式化的檔案。--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-inputCLI 用法
Section titled “CLI 用法”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 © 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.