Some of the features of Jinja are:9
Jinja, like Smarty, also ships with an easy-to-use filter system similar to the Unix pipeline.
The syntax for printing output in Jinja is using the double curly braces, for example {{ Hello, World! }}.
Statements which set variables in jinja or those which do not have an output can be wrapped within {% and %}, using the set keyword. For example {% set foo = 42 %} sets a variable called foo with a value of 42.
Similar to above, comments in jinja can be written using a number sign (#) instead of a percentage (%), for example, {# helpful comment #}.
The syntax for creating a filter in Jinja is a vertical bar (|), for example {{ variable|filter }}. A variable can have multiple filters, for example {{ variable|filter|filter }}).10
The syntax for creating a test in Jinja is the keyword is as well as the conditions for evaluating the validity of a test, such as for example {% if variable is divisibleby 10 %}do something{% endif %}).11
For loops can be used to iterate over sequences, while retaining their object properties. The following example demonstrates iterating over a list of users with username and password fields.
Although break and continue are not allowed inside loops, sequences can be filtered.
Here is a small example of a template file example.html.jinja:12
and templating code:
This produces the HTML string:
Note the minus sign (-) after the tag {%: If you add a minus sign (-) to the start or end of a block (e.g. a For tag), a comment, or a variable expression, the whitespaces before or after that block will be removed.13
"Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26. https://jinja.palletsprojects.com/en/2.11.x/extensions/#module-jinja2.ext ↩
"Jinja built-in filters and tests (like Django filters)". www.webforefront.com. Retrieved 2023-08-14. https://www.webforefront.com/django/usebuiltinjinjafilters.html ↩
"Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26. https://jinja.palletsprojects.com/en/2.11.x/extensions/#expression-statement ↩
DuPlain, R. (2013). Instant Flask Web Development. Packt Publishing. p. 30. ISBN 978-1-78216-963-5. Retrieved 2015-05-26. 978-1-78216-963-5 ↩
"Templating (Jinja2) — Ansible Documentation". https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html ↩
"Understanding Jinja". docs.saltproject.io. https://docs.saltproject.io/en/latest/topics/jinja/index.html ↩
Jinja and macros | dbt Developer Hub https://docs.getdbt.com/docs/build/jinja-macros ↩
"Welcome | Jinja2 (The Python Template Engine)". palletsprojects.com/p/jinja. https://palletsprojects.com/p/jinja/ ↩
Ronacher, Armin. "Template Designer Documentation". Jinja Documentation (3.0.x). Retrieved 9 January 2024. A Jinja template doesn't need to have a specific extension: .html, .xml, or any other extension is just fine. https://jinja.palletsprojects.com/en/3.0.x/templates/ ↩
"Template Designer Documentation — Jinja Documentation (3.0.x)". jinja.palletsprojects.com. Retrieved 2024-01-09. https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control ↩