Erste Schritte

Installation

djLint ist in Python geschrieben und auf PyPI veröffentlicht. Mit uv fügen Sie es Ihrem Projekt als Entwicklungsabhängigkeit hinzu:

uv add --dev djlint

Oder installieren Sie es als eigenständiges Tool:

uv tool install djlint

Oder mit pip:

pip install djlint

Oder mit pipx:

pipx install djlint

Oder unter macOS oder Linux mit der von der Community gepflegten Homebrew-Formel:

brew install djlint

Oder mit npm. Achtung: Das npm-Paket ist nur ein Wrapper. Sein Installationsskript führt pip mit dem python3 aus, das gerade in Ihrem Systempfad liegt, und installiert die djLint-Version dieses npm-Releases, die ein neueres djLint durch ein älteres ersetzen kann. npm verwaltet das eigentliche Paket nicht und deinstalliert es auch nicht, verwenden Sie daher nach Möglichkeit pip.

npm i djlint

Wie Sie djLint bei jedem Commit ausführen, lesen Sie unter pre-commit.

Verwendung auf der Befehlszeile

djLint ist ein Befehlszeilenprogramm. Für die erweiterte Konfiguration siehe die Seite Konfiguration.

Usage: djlint [OPTIONS] SRC ...

  djLint · HTML template linter and formatter.

Options:
  --version                       Show the version and exit.
  --lsp                           Run as a language server over stdio, for
                                  editors.
  -e, --extension TEXT            File extension to check [default: html]
  -i, --ignore TEXT               Codes to ignore. ex: "H014,H017"
  --reformat                      Reformat the file(s).
  --check                         Check formatting on the file(s).
  --stdin-filename TEXT           Filename to use for per-file-ignores and
                                  messages when reading from stdin. [default: -]
  --indent INTEGER                Indent spacing. [default: 4]
  --quiet                         Do not print diff when reformatting.
  --profile TEXT                  Enable defaults by template language. ops:
                                  django, jinja, nunjucks, handlebars, liquid,
                                  golang, angular, tera, askama, html [default:
                                  html]
  --require-pragma                Only format or lint files that starts with a
                                  comment with the text 'djlint:on'
  --lint                          Lint for common issues. [default option]
  --use-gitignore                 Use .gitignore file to extend excludes.
  --allow-empty-input             Exit with 0 instead of 2 when the given paths
                                  match no files.
  --warn                          Return errors as warnings.
  --preserve-leading-space        Attempt to preserve leading space on text.
  --preserve-blank-lines          Attempt to preserve blank lines.
  --preserve-class-newlines       Preserve line breaks inside multiline class
                                  attributes.
  --format-css                    Also format contents of <style> tags.
  --format-js                     Also format contents of <script> tags.
  --format-attribute-js-json      Also format JavaScript/JSON inside HTML
                                  attributes.
  --format-attribute-js-json-pattern TEXT
                                  Regex pattern to match JavaScript attributes.
  --format-attribute-js-json-min-props INTEGER
                                  Minimum number of properties to treat
                                  attribute content as JS/JSON.
  --configuration FILE            Path to global configuration file in
                                  djlint.toml, .djlint.toml, or .djlintrc format
  --prefer-configuration          Let --configuration override the project's own
                                  config file, rather than the other way round.
  --rules FILE                    Path to custom rules file in
                                  .djlint_rules.yaml format
  --statistics                    Count the number of occurrences of each
                                  error/warning code.
  --include TEXT                  Codes to include. ex: "H014,H017"
  --ignore-case                   Do not fix case on known html tags.
  --ignore-blocks TEXT            Comma list of template blocks to not indent.
  --blank-line-after-tag TEXT     Add an additional blank line after {% <tag>
                                  ... %} tag groups.
  --blank-line-before-tag TEXT    Add an additional blank line before {% <tag>
                                  ... %} tag groups.
  --line-break-after-multiline-tag
                                  Do not condense the content of multi-line tags
                                  into the line of the last attribute.
  --custom-blocks TEXT            Indent custom template blocks. For example {%
                                  toc %}...{% endtoc %}
  --custom-html TEXT              Indent custom HTML tags. For example <mjml>
  --exclude TEXT                  Override the default exclude paths.
  --extend-exclude TEXT           Add additional paths to the default exclude.
  --linter-output-format TEXT     Customize order of linter output message.
  --max-line-length INTEGER       Max line length. [default: 120]
  --max-attribute-length INTEGER  Max attribute length. [default: 70]
  --format-attribute-template-tags
                                  Attempt to format template syntax inside of
                                  tag attributes.
  --single-attribute-per-line     When an opening tag wraps, put each attribute
                                  on its own line.
  --per-file-ignores <TEXT TEXT>...
                                  Ignore linter rules on a per-file basis.
  --indent-css INTEGER            Set CSS indent level.
  --indent-js INTEGER             Set JS indent level.
  --close-void-tags               Add closing mark on known void tags. Ex: <img>
                                  becomes <img />
  --no-line-after-yaml            Do not add a blank line after yaml front
                                  matter.
  --name-endblocks                Name the endblock of a block written across
                                  lines.
  --sort-attributes               Sort attributes by name, with id first and
                                  class second.
  --sort-load-tags                Sort the libraries in {% load %} tags.
  --no-indent-inner-html          Do not indent <head> and <body> below <html>.
  --no-function-formatting        Do not attempt to format function contents.
  --no-set-formatting             Do not attempt to format set contents.
  --keep-br-inline                Keep <br> on the line of the text it breaks.
  --no-entity-formatting          Do not rewrite entity references as
                                  characters.
  --quote-style [double|single]   Quotes to use for strings inside template
                                  tags. [default: double]
  --max-blank-lines INTEGER       Consolidate blank lines down to x lines.
                                  [default: 0]
  --github-output / --no-github-output
                                  Output GitHub-compatible formatting.
  --sarif                         Output findings as SARIF 2.1.0 json, for
                                  GitHub code scanning and other tools.
  -h, --help                      Show this message and exit.

Hinweis

Wird der Befehl djlint nicht gefunden, prüfen Sie, ob Python in Ihrem PATH liegt.

Pfad oder stdin

djLint arbeitet mit einem Pfad oder mit stdin.

Mit einem Pfad:

djlint /path/to/templates --lint

Oder mit einer bestimmten Datei:

djlint /path/to/this.mustache --lint

Oder mit stdin:

echo "<div></div>" | djlint -

Über stdin lässt sich Code auch neu formatieren. Ausgegeben wird dann nur der formatierte Code, ohne Meldungen.

echo "<div></div>" | djlint - --reformat

Ausgabe:

<div></div>

Beim Lesen von stdin hat djLint keinen echten Pfad, mit dem es per-file-ignores abgleichen oder den es in Meldungen anzeigen könnte, und verwendet deshalb standardmäßig -. Mit --stdin-filename teilen Sie djLint den echten Pfad des übergebenen Inhalts mit, zum Beispiel wenn eine Editor-Integration beim Speichern den Inhalt einer Datei per Pipe übergibt.

echo "<div></div>" | djlint - --stdin-filename templates/index.html

Exit-Codes

Code Bedeutung
0 Alles, was djLint prüfen sollte, ist fehlerfrei, oder jede gefundene Datei wurde durch Ihre Konfiguration übersprungen.
1 djLint hat Linting-Fehler gefunden oder Dateien, die neu formatiert werden müssen. Mit --warn werden diese als Warnungen gemeldet und der Exit-Code ist 0.
2 djLint hat nicht geprüft, was Sie verlangt haben: Die Pfade passten auf keine Datei, die Befehlszeile oder die Konfiguration war ungültig, oder djLint ist fehlgeschlagen.

Nur Code 1 bedeutet, dass djLint sich Ihre Templates angesehen hat und mit dem Ergebnis nicht zufrieden ist. Code 2 bedeutet immer, dass der Lauf selbst nicht getan hat, was er sollte. So kann eine Pipeline die beiden Fälle unterschiedlich behandeln.

Code 2 fängt einen Lauf ab, der überhaupt nichts geprüft hat: ein falscher Pfad, eine --extension, die nicht mehr passt, oder Templates, die verschoben wurden. Ohne ihn geht ein solcher Lauf stillschweigend durch.

Dateien, die djLint durchaus gefunden und dann absichtlich übersprungen hat, über exclude, extend_exclude, use_gitignore oder require_pragma, sind kein Fehler. Ein solcher Lauf endet mit 0, denn die Konfiguration hat genau das getan, was sie sollte. Dadurch funktioniert auch exclude unter pre-commit, das die Namen der Dateien in Ihrer Staging-Area übergibt, ob djLint sie sich ansehen soll oder nicht.

Ist ein Pfad, der berechtigterweise keine Templates enthält, in Ihrer Pipeline normal, schalten Sie Code 2 mit allow_empty_input ab:

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