What's with all the curly brackets? Jinja template delimiters and whitespace control

Delimiters

Jinja uses various delimiters symbols in the template strings.

{{ }} - Expressions produce a value which is printed as the template output.

{% %} - Statements, these do not output anything. They are instructions for the rest of the template. Statements are used for control structures like conditionals (i.e. if/elif/else) and for-loops; as well as macros, assigning values to variables, and importing custom functions.

{# #} - Comments are used to leave yourself notes. They are not included in the template’s printed output, nor are they executed as instructions.

Whitespace Control

Adding a - after a starting delimiter will remove previous whitespace. Adding a - before an ending delimiter will remove trailing white space.

{%- Removes leading white space before a statement.

-}} Removes trailing white space after an expression that prints to the template output.

Jinja docs - Whitespace Control

7 Likes