What is the Jinja indent Filter?
nThe Jinja indent filter is a powerful tool for formatting multi-line strings by adding a specific indentation to each line. It’s particularly useful for generating well-structured output like code, configuration files, or nested HTML, where proper indentation is crucial for readability. By default, the filter adds four spaces to each line, but it gives you flexible control over the indentation width, the characters used, and whether to indent the first and blank lines.nn
nn
How the indent Filter Works
nThe indent filter works by iterating over a string, line by line, and adding a specified number of spaces or a custom string at the beginning of each line.nnThe syntax is as follows:n
{{ my_string | indent(width=4, first=False, blank=False) }}
nIt accepts three optional parameters:n
- n
width(int or string, default4): This defines the indentation. You can provide an integer for the number of spaces or a string for a custom indentation character (e.g.,indent(width=' ')for two spaces, orindent(width='--')for two hyphens).first(boolean, defaultFalse): By default, theindentfilter does not indent the first line of the string. Setting this parameter toTruewill ensure that the first line is also indented.blank(boolean, defaultFalse): The filter typically skips blank lines. Settingblank=Truewill ensure that even empty lines are indented.
n
n
n
nn
nn
Practical Examples
nLet’s look at some examples to see how the indent filter can be used to format text effectively.n
Example 1: Basic Indentation
nThis is the default behavior, where the first line and blank lines are not indented.nnJinja Template:n
{% set my_text = "This is a line.\nThis is another line.\n\nA final line." %}n<pre>n{{ my_text | indent }}n</pre>
nRendered HTML:n
<pre>nThis is a line.n This is another line.nn A final line.n</pre>
nAs you can see, only the second and fourth non-empty lines are indented.n
Example 2: Indenting the First Line
nBy setting first=True, you can indent the entire block of text. This is often used when nesting a block of code within another structure.nnJinja Template:n
{% set my_text = "This is a line.\nThis is another line." %}n<pre>n<p>n This is some code:n{{ my_text | indent(width=8, first=True) }}n</p>n</pre>
nRendered HTML:n
<pre>n<p>n This is some code:n This is a line.n This is another line.n</p>n</pre>
nHere, we use a width of 8 spaces to align the indented block with the code that precedes it, and first=True ensures the very first line is also indented.n
Example 3: Customizing Indentation with Blank Lines
nThis example shows how to use a custom indentation string and indent blank lines.nnJinja Template:n
{% set my_text = "This is a line.\n\nAnother line." %}n<pre>n{{ my_text | indent(width='---', blank=True) }}n</pre>
nRendered HTML:n
<pre>nThis is a line.n---n---Another line.n</pre>
nNotice how a blank line is now indented with the custom string, but the first line is still not indented since first is still False.nn
nn
Conclusion
nThe Jinja indent filter is a powerful formatting utility that gives you fine-grained control over the presentation of multi-line strings. By using its parameters to adjust the indentation width, characters, and which lines to indent, you can ensure your generated text is clean, readable, and properly structured. It’s a key tool for creating dynamic, formatted content within your templates.nn
