CARVIEW |
Running Prettier against Django or Jinja templates
I really like auto-formatting tools like Black. I've been hoping to find one that works with Django and Jinja templates for years.
Today I managed to get excellent JavaScript Prettier formatter to run against a Jinja template file using the prettier-plugin-jinja-template plugin by David Odenwald.
I had a bit of a fiddle getting it to work because I'm still not fluent in npm
/npx
, but the recipe I used was the following.
-
Install
prettier
andprettier-plugin-jinja-template
in a directory somewhere. This command will create apackage.json
andpackage-lock.json
and a a wholenode_modules/
folder - the full install adds up to 8.4M:npm i prettier prettier-plugin-jinja-template
-
In that directory run this command:
npx prettier --plugin=prettier-plugin-jinja-template \ --parser=jinja-template \ --write path/to/your/template.html
The
--write
option will rewrite the template in place.
I first tried using npm i -g prettier prettier-plugin-jinja-template
to install the application once, globally, but I couldn't work out how to get the plugin working that way.
Instead, I've added it to my path another way. I already have a ~/.local/bin/
directory that is on my $PATH
, so I ran the npm i
command from above in that folder and then added this script there, in a file called pretty-templates.sh
(created with the help of Claude):
#!/bin/bash
# Check if at least one argument is provided
if [ $# -eq 0 ]; then
echo "Error: At least one argument is required."
exit 1
fi
# Store the current directory
original_dir=$(pwd)
# Change directory to ~/.local/bin/
cd ~/.local/bin/ || exit
# Convert all paths to absolute paths
args=()
for arg in "$@"; do
args+=("$original_dir/$arg")
done
# Run the prettier command with the absolute paths
npx prettier --plugin=prettier-plugin-jinja-template \
--parser=jinja-template \
--write "${args[@]}"
Now I can run that command from anywhere on my computer:
prettier-templates.sh templates/team_backups.html
Jeff Triplett pointed me to two pure Python alternatives:
-
curlylint is "experimental HTML templates linting for Jinja, Nunjucks, Django templates, Twig, Liquid" -
pip install curlylint
thencurlylint my/templates
-
DjHTML is "a pure-Python Django/Jinja template indenter without dependencies" -
pip install djhtml
thendjhtml template.html
Related
- github-actions Using Prettier to check JavaScript code style in GitHub Actions - 2020-12-31
- yaml Auto-formatting YAML files with yamlfmt - 2023-07-13
- django Using just with Django - 2022-06-06
- javascript Minifying JavaScript with npx uglify-js - 2020-08-30
- javascript Using Jest without a package.json - 2020-12-30
- sphinx Format code examples in documentation with blacken-docs - 2022-04-24
- observable Using jq in an Observable notebook - 2023-03-25
- javascript Using packages from JSR with esbuild - 2024-03-02
- datasette Syntax highlighted code examples in Datasette - 2023-07-01
- github-actions Using the GitHub Actions cache with npx and no package.json - 2022-03-22
Created 2024-06-19T19:32:36-07:00, updated 2024-06-19T20:56:12-07:00 · History · Edit