시작하기

설치

djLint는 Python으로 만들어졌으며 PyPI에 게시되어 있습니다. uv를 사용한다면 프로젝트에 개발 의존성으로 추가합니다.

uv add --dev djlint

또는 독립 실행형 도구로 설치합니다.

uv tool install djlint

또는 pip으로 설치합니다.

pip install djlint

또는 pipx로 설치합니다.

pipx install djlint

또는 macOS나 Linux에서는 커뮤니티가 관리하는 Homebrew 포뮬러로 설치합니다.

brew install djlint

또는 npm으로 설치합니다. 경고: npm 패키지는 래퍼일 뿐입니다. 설치 스크립트는 시스템 경로에 있는 python3이 무엇이든 그것으로 pip을 실행해 해당 npm 릴리스에 맞는 djLint 버전을 설치하므로, 더 새로운 djLint가 이전 버전으로 바뀔 수 있습니다. npm은 실제 패키지를 관리하거나 제거하지 않으므로 가능하면 pip을 사용하세요.

npm i djlint

커밋할 때마다 djLint를 실행하려면 pre-commit을 참고하세요.

CLI 사용법

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 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.

참고

djlint 명령을 찾을 수 없다면 Python이 PATH에 있는지 확인하세요.

경로와 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에서 읽을 때는 per-file-ignores와 대조하거나 메시지에 표시할 실제 경로가 없으므로, djLint는 기본값으로 -를 사용합니다. 예를 들어 에디터 통합이 저장할 때 파일 내용을 파이프로 넘기는 경우처럼, 파이프로 넘긴 내용의 실제 경로를 djLint에 알려 주려면 --stdin-filename을 전달하세요.

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으로 종료합니다. 덕분에 pre-commit에서도 exclude가 동작합니다. pre-commit은 djLint가 살펴보기를 원하든 원하지 않든 스테이징된 파일의 이름을 넘기기 때문입니다.

템플릿이 없는 것이 정상인 경로를 파이프라인에서 다룬다면 allow_empty_input으로 코드 2를 끄세요.

djlint /path/to/templates --lint --allow-empty-input