Prise en main
Installation
Section intitulée « Installation »djLint est sur PyPI, où il demande Python 3.11 ou plus récent, et sur npm, qui embarque son propre Python. Dans un projet Python, ajoutez-le avec uv ou pip. Dans un projet JavaScript, comme une application Angular, un site Eleventy ou un thème Shopify, avec npm.
Avec uv, ajoutez-le à votre projet comme dépendance de développement :
uv add --dev djlintOu installez-le comme outil autonome :
uv tool install djlintpip install djlintLe paquet npm embarque son propre Python : il lui suffit de Node.js 16.9 ou plus récent. Il existe des builds pour Linux sur x64, Arm64, ppc64le, s390x et riscv64, macOS sur x64 et Arm64, et Windows sur x64, x86 et Arm64. Sous Linux avec musl, comme Alpine, il tourne sur x64 et Arm64. Ajoutez-le à votre projet comme dépendance de développement :
npm i -D djlintOu installez-le globalement :
npm i -g djlintAvec pipx :
pipx install djlintAvec la formule Homebrew maintenue par la communauté, sous macOS ou Linux :
brew install djlintPour exécuter djLint dans votre éditeur ou à chaque commit, consultez Intégrations et pre-commit.
Premier lancement
Section intitulée « Premier lancement »Indiquez à djLint le dossier de vos templates et passez à --profile le langage de vos templates. Le profil décide des règles du linter qui s’appliquent et de la façon dont la syntaxe des templates est formatée. Pour un projet Django :
Lintez les templates (c’est le mode par défaut, vous pouvez donc omettre --lint) :
djlint templates --lint --profile=djangoVoyez ce que le formatage changerait. Avec --check, djLint affiche un diff sans toucher aux fichiers :
djlint templates --check --profile=djangoReformatez les fichiers sur place :
djlint templates --reformat --profile=djangoL’option profile liste tous les profils. Chaque langage de templates a aussi sa propre page, par exemple Django, Jinja ou Handlebars, qui montre comment définir le profil dans un fichier de configuration pour ne pas avoir à le passer à chaque fois.
Si vous avez ajouté djLint au projet avec uv add --dev, il se trouve dans l’environnement du projet : préfixez les commandes par uv run, par exemple uv run djlint templates --lint --profile=django. Avec npm i -D, préfixez-les plutôt par npx, par exemple npx djlint templates --lint --profile=django.
Chemin ou stdin
Section intitulée « Chemin ou stdin »djLint accepte un chemin ou l’entrée standard (stdin).
Avec un chemin :
djlint /path/to/templates --lintOu un fichier précis :
djlint /path/to/this.mustache --lintOu avec stdin :
echo "<div></div>" | djlint -L’entrée standard permet aussi de reformater du code. La sortie ne contient alors que le code formaté, sans aucun message.
echo "<div></div>" | djlint - --reformatSortie :
<div></div>Quand il lit l’entrée standard, djLint n’a pas de vrai chemin à comparer à per-file-ignores ni à afficher dans les messages : il utilise donc - par défaut. Passez --stdin-filename pour indiquer à djLint le vrai chemin du contenu reçu, par exemple quand une intégration d’éditeur lui transmet le contenu d’un fichier à l’enregistrement.
echo "<div></div>" | djlint - --stdin-filename templates/index.htmlCodes de sortie
Section intitulée « Codes de sortie »| Code | Signification |
|---|---|
0 |
Tout ce que djLint devait vérifier est correct, ou chaque fichier trouvé a été ignoré par votre configuration. |
1 |
djLint a trouvé des erreurs de linting, ou des fichiers à reformater. Avec --warn, ils sont signalés comme des avertissements et le code de sortie est 0. |
2 |
djLint n’a pas vérifié ce que vous lui avez demandé : les chemins ne correspondent à aucun fichier, un fichier n’a pas pu être lu (pas en UTF-8, ou sans permission), la ligne de commande ou la configuration est invalide, ou djLint a échoué. |
Seul le code 1 signifie que djLint a examiné vos templates et n’a pas aimé ce qu’il y a trouvé : un pipeline peut donc distinguer une vérification qui échoue d’une exécution qui n’a rien vérifié, par exemple avec un chemin erroné ou une --extension qui ne correspond plus.
Les fichiers que djLint a trouvés puis volontairement ignorés, via exclude, extend_exclude, use_gitignore ou require_pragma, ne constituent pas une erreur, et c’est ce qui permet à exclude de fonctionner avec pre-commit. Si, dans votre pipeline, il est normal qu’un chemin ne contienne aucun template, désactivez le code 2 avec allow_empty_input :
djlint /path/to/templates --lint --allow-empty-inputUtilisation en ligne de commande
Section intitulée « Utilisation en ligne de commande »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.