Skip to content

djlint

GitHub stars sarif GitHub release (latest SemVer) GitHub last commit GitHub commit activity GitHub contributors

djLint is a comprehensive linter and formatter for HTML templates that supports multiple template languages. Originally created for use in the Atlas projects, it helps you find common syntax errors and reformat your HTML templates to make them shine.

Key Features:

  • Multi-Language Support: Supports Django, Jinja, Twig, Nunjucks, Handlebars, Mustache, Liquid, Golang templates, Angular, Tera, Askama and Mako
  • Comprehensive Rule Set: 60+ built-in rules covering HTML standards, accessibility, security, and template-specific best practices
  • Language-Specific Rules: Specialized rules for Django (D-codes), Jinja (J-codes), HTML (H-codes), and general templates (T-codes)
  • Auto-Formatting: Automatically formats and beautifies HTML templates with consistent indentation and structure
  • Configurable Profiles: Pre-configured profiles for different template languages with appropriate rule sets
  • Custom Rules: Support for custom pattern-based and Python module rules for project-specific requirements
  • Flexible Configuration: Enable/disable rules via command line, configuration files, or inline comments

For example, define HTML_DJLINT_ARGUMENTS: ["--profile", "django"] to select django format

djlint documentation

djLint - GitHub

Configuration in MegaLinter

Variable Description Default value
HTML_DJLINT_ARGUMENTS User custom arguments to add in linter CLI call
Ex: -s --foo "bar"
HTML_DJLINT_COMMAND_REMOVE_ARGUMENTS User custom arguments to remove from command line before calling the linter
Ex: -s --foo "bar"
HTML_DJLINT_FILTER_REGEX_INCLUDE Custom regex including filter
Ex: (src\|lib)
⚠️ Not available with HTML_DJLINT_CLI_LINT_MODE = project
Exclude no file
HTML_DJLINT_FILTER_REGEX_EXCLUDE Custom regex excluding filter
Ex: (test\|examples)
⚠️ Not available with HTML_DJLINT_CLI_LINT_MODE = project
Exclude no file
HTML_DJLINT_CLI_LINT_MODE Override default CLI lint mode
- file: Calls the linter for each file
- list_of_files: Call the linter with the list of files as argument
- project: Call the linter from the root of the project
list_of_files
HTML_DJLINT_FORWARD_EXCLUDED_DIRECTORIES In project CLI lint mode, forward excluded directories (EXCLUDED_DIRECTORIES + ADDITIONAL_EXCLUDED_DIRECTORIES, and .gitignored directories when IGNORE_GITIGNORED_FILES is true) to the linter through its native exclusion arguments or generated ignore/config files true
HTML_DJLINT_FILE_EXTENSIONS Allowed file extensions. "*" matches any extension, "" matches empty extension. Empty list excludes all files
Ex: [".py", ""]
[".html", ".htm"]
HTML_DJLINT_FILE_NAMES_REGEX File name regex filters. Regular expression list for filtering files by their base names using regex full match. Empty list includes all files
Ex: ["Dockerfile(-.+)?", "Jenkinsfile"]
Include every file
HTML_DJLINT_PRE_COMMANDS List of bash commands to run before the linter None
HTML_DJLINT_POST_COMMANDS List of bash commands to run after the linter None
HTML_DJLINT_UNSECURED_ENV_VARIABLES List of env variables explicitly not filtered before calling HTML_DJLINT and its pre/post commands None
HTML_DJLINT_DISABLE_ERRORS Run linter but consider errors as warnings false
HTML_DJLINT_DISABLE_ERRORS_IF_LESS_THAN Maximum number of errors allowed 0
HTML_DJLINT_TIMEOUT_SECONDS Maximum duration in seconds of the linter run, after which the linter process and its child processes are killed and reported as an error (exit code 124). Overrides LINTER_TIMEOUT_SECONDS. 0 disables the timeout 300
HTML_DJLINT_CLI_EXECUTABLE Override CLI executable ['djlint']

IDE Integration

Use djlint in your favorite IDE to catch errors before MegaLinter !

IDE Extension Name Install
Sublime Text SublimeLinter-contrib-djlint Visit Web Site
Visual Studio Code djlint-vscode Install in VSCode

MegaLinter Flavors

This linter is available in the following flavors

Flavor Description Embedded linters Info
all Default MegaLinter Flavor 135 Docker Image Size (tag) Docker Pulls
c_cpp Optimized for pure C/C++ projects 60 Docker Image Size (tag) Docker Pulls
cupcake MegaLinter for the most commonly used languages 100 Docker Image Size (tag) Docker Pulls
documentation MegaLinter for documentation projects 53 Docker Image Size (tag) Docker Pulls
dotnet Optimized for C, C++, C# or VB based projects 67 Docker Image Size (tag) Docker Pulls
dotnetweb Optimized for C, C++, C# or VB based projects with JS/TS 82 Docker Image Size (tag) Docker Pulls
go Optimized for GO based projects 55 Docker Image Size (tag) Docker Pulls
java Optimized for JAVA based projects 58 Docker Image Size (tag) Docker Pulls
javascript Optimized for JAVASCRIPT or TYPESCRIPT based projects 69 Docker Image Size (tag) Docker Pulls
php Optimized for PHP based projects 58 Docker Image Size (tag) Docker Pulls
python Optimized for PYTHON based projects 71 Docker Image Size (tag) Docker Pulls
ruby Optimized for RUBY based projects 54 Docker Image Size (tag) Docker Pulls
rust Optimized for RUST based projects 54 Docker Image Size (tag) Docker Pulls
salesforce Optimized for Salesforce based projects 59 Docker Image Size (tag) Docker Pulls
swift Optimized for SWIFT based projects 54 Docker Image Size (tag) Docker Pulls
terraform Optimized for TERRAFORM based projects 58 Docker Image Size (tag) Docker Pulls

Behind the scenes

How are identified applicable files

  • File extensions: .html, .htm

How the linting is performed

djlint is called once on the whole project directory (project CLI lint mode)

  • filtering can not be done using MegaLinter configuration variables,it must be done using djlint configuration or ignore file (if existing)
  • VALIDATE_ALL_CODEBASE: false doesn't make djlint analyze only updated files

Example calls

djlint myfile1.html myfile2.html

Help content

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. [default:
                                  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 that are off by
                                  default, comma-separated, for example H017.
  --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.
  SRC ...
  -h, --help                      Show this message and exit.

Installation on mega-linter Docker image

  • Dockerfile commands :
    # renovate: datasource=pypi depName=djlint
    ARG PIP_DJLINT_VERSION=1.49.10
    

Installation without Docker (native mode)

This linter can run without Docker, see native mode.

  • Install method: Python package (isolated virtual environment)
  • Requires the python runtime >=3.11
NATIVE_LINTERS: [HTML_DJLINT]
Platform Support
Linux (x86-64) ✅
Linux (ARM64) ✅
macOS (Intel) ✅
macOS (Apple Silicon) ✅
Windows (Git Bash) ✅
INSTALL_DIR="$HOME/.megalinter/linters/HTML_DJLINT"
uv venv "$INSTALL_DIR" --python ">=3.11"
uv pip install --python "$INSTALL_DIR/bin/python" --only-binary :all: "djlint==1.49.10"
export PATH="$INSTALL_DIR/bin:$PATH"
INSTALL_DIR="$HOME/.megalinter/linters/HTML_DJLINT"
uv venv "$INSTALL_DIR" --python ">=3.11"
uv pip install --python "$INSTALL_DIR/Scripts/python.exe" --only-binary :all: "djlint==1.49.10"
export PATH="$INSTALL_DIR/Scripts:$PATH"

Comments