Jinja (template engine) explained

Jinja
Author:Armin Ronacher
Logo Size:200px
Released:[1]
Programming Language:Python
Genre:Template engine
License:BSD License

Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox. It is a text-based template language and thus can be used to generate any markup as well as source code.

The Jinja template engine allows customization of tags,[2] filters (for formatting or transforming values[3]), tests (for evaluating conditions[3]), and globals.[4] Also, unlike the Django template engine, Jinja allows the template designer to call functions with arguments on objects.Jinja is Flask's default template engine [5] and it is also used by Ansible,[6] Trac, and Salt.[7] It is also used to make SQL macros, for example for use with dbt.[8]

Features

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.

Syntax

The syntax for printing output in Jinja is using the double curly braces, for example <nowiki>{{ Hello, World! }}</nowiki>.

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 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, <nowiki>{# helpful comment #}</nowiki>.

The syntax for creating a filter in Jinja is a vertical bar (|), for example <nowiki>{{ variable|filter }}</nowiki>. A variable can have multiple filters, for example <nowiki>{{ variable|filter|filter }}</nowiki>).

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 do something).

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 and fields. Although and are not allowed inside loops, sequences can be filtered.

Example

Here is a small example of a template file example.html.jinja:[10] , and templating code:from jinja2 import Templatewith open('example.html.jinja') as f: tmpl = Template(f.read)print(tmpl.render(variable = 'Value with data', item_list = [1, 2, 3, 4, 5, 6]))This produces the HTML string: Value with <unsafe> data 1, 2, 3, 4, 5, 6 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.[11]

Notes and References

  1. Web site: Jinja2 Release History . 24 June 2020.
  2. Web site: Extensions . Jinja2 Documentation (2.8-dev) . 2015-05-26.
  3. Web site: 2023-08-14. Jinja built-in filters and tests (like Django filters). www.webforefront.com.
  4. Web site: Extensions . Jinja2 Documentation (2.8-dev) . 2015-05-26.
  5. Book: DuPlain, R. . Instant Flask Web Development . Packt Publishing . 2013 . 978-1-78216-963-5 . 2015-05-26 . 30.
  6. Web site: Templating (Jinja2) — Ansible Documentation.
  7. Web site: Understanding Jinja. docs.saltproject.io.
  8. https://docs.getdbt.com/docs/build/jinja-macros Jinja and macros | dbt Developer Hub
  9. Web site: Welcome | Jinja2 (The Python Template Engine). palletsprojects.com/p/jinja.
  10. Web site: Ronacher . Armin . Template Designer Documentation . 9 January 2024 . Jinja Documentation (3.0.x) . A Jinja template doesn’t need to have a specific extension: .html, .xml, or any other extension is just fine..
  11. Web site: Template Designer Documentation — Jinja Documentation (3.0.x) . 2024-01-09 . jinja.palletsprojects.com.