Standardize Django Template Indentation with DjHTML from Linux command line or PyCharm
What is DjHTML?
DjHTMLis a pure-Python Django/Jinja template indenter without dependencies.
It standardizes the indentation for Django or Jinja templates throughout your Django project. DjHTML (intentionally) only works on one file at a time. This simplifies DjHTML development and prevents issues with trying to support differences in directory navigation for each operating system. You can read more about DjHTML at it's GitHub repository .
In order to operate on multiple files or entire directories, we can use built-in tools to apply DjHTML to multiple files.
This short guide assumes you are working with Linux and that you have followed the installation instructions for DjHTML. If you are working with a different operating system, it may get you started in the right direction.
Using DjHTML on a directory of templates from the command line
Because DjHTML only takes action on a single file on its own, we need to combine a couple of built-in command line tools within Linux to use it with multiple files at once:
xargs, and piping.
The find command lets us search for files that meet certain criteria. In our case, we want to search within the current directory (and any subdirectories) for all files that end in .html. The single period "." in the example below tells the
find command to search starting from the current directory. You can replace the period with any valid relative or absolute path.
-name argument of the
find command is where we specify the criteria for the file names we want returned by the command. Here, we use the wildcard "*" to specify that we want any files that end in .html
We then use a pipe "|", which redirects the output of one command, program, or process as input to another. Here, we are passing any files found by the
find command to the
xargs command for further processing.
The xargs command allows you to build up and execute commands with arguments from standard input. Each output of the
find command (each file that was 'found') will be passed as an argument to
xargs, which will then apply
DjHTML indentation to it.
Combining all of this, to apply DjHTML to all html files in the current directory, use the following:
find . -name "*.html" | xargs djhtml -i
Take a look at this issue on DjHTML's GitHub Issues and read through the links provided above on each command for more information.
Using DjHTML in PyCharm
Indent Whenever an HTML File Changes
PyCharm has a file watcher functionality that allows you to apply actions whenever a file changes and is saved. We can add a new File Watcher to automatically apply indentation when a changes are made to a file.
In order to identify the installed location of DjHTML, which you'll need below, use the following on the command-line:
To add the File Watcher, from the File menu in PyCharm select Settings. The Settings window will open with a hierarchical menu on the left side.
Within that menu, navigate to Tools and select File Watchers.
In the File Watchers panel, select the + button near the top and from the drop-down, select < custom >. The Create File Watcher dialog will open.
You should be able to use the settings below.
File Type: HTML
Scope: Project Files
Program: * Full path to your install of DjHTML *
Arguments: -i $FilePath$
Output Paths: $ProjectFileDir$
Be sure that "Trigger the watcher on external changes" is checked.
It should look something like this:
Click Ok on the Create File Watcher and Settings dialog windows.
Any time you change an html file in your project and save it, DjHTML should automatically apply indentation.
Use External Tools to Apply Indentation as Needed
External Tools in PyCharm makes it easy to apply actions from third-party tools (like DjHTML) to arbitrary files or directories of files in your project.
From the File menu in PyCharm select Settings. The Settings window will open with a hierarchical menu on the left side.
Within that menu, navigate to Tools and select External Tools.
In the External Tools panel, select the + button near the top to add a new External Tool. The Create Tool dialog will open.
Because of oddities with entering arguments in PyCharm's External Tools, you'll need to pass the arguments to the bash command, rather than running DjHTML directly. If your copy of
bash is at a non-standard location, you can identify the location by using the following command:
You should be able to use the settings below, modifying the path to
bash, if needed.
Description: Django/Jinja template indenter
Arguments: -c "find '$FilePath$' -name '*.html' | xargs djhtml -i"
Working Directory: $ProjectFileDir$
What it should look like within PyCharm:
Click Ok on the Create Tool and Settings dialog windows.
Now you should be able to right-click on any directory or file in the Project pane of the PyCharm IDE, select External Tools -> DjHTML, and the selected files will be indented and saved.
Congratulations! Now you can easily standardize the indentation of all templates within your Django projects!