Jinja (template engine)

From Wikipedia, the free encyclopedia
Original author(s)Armin Ronacher
Initial releaseJuly 17, 2008; 14 years ago (2008-07-17)[1]
Stable release
3.1.2[2] Edit this on Wikidata / 28 April 2022; 12 months ago (28 April 2022)
Written inPython
TypeTemplate engine
LicenseBSD License
Websitepalletsprojects.com/p/jinja/ Edit this on Wikidata

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,[3] filters, tests, 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]


Some of the features of Jinja are:[8]

  • sandboxed execution
  • automatic HTML escaping to prevent cross-site scripting (XSS) attacks
  • template inheritance
  • compiles down to the optimal Python code just-in-time
  • optional ahead-of-time template compilation
  • easy to debug (for example, line numbers of exceptions directly point to the correct line in the template)
  • configurable syntax

Jinja, like Smarty, also ships with an easy-to-use filter system similar to the Unix pipeline.


Here is a small example of a template file example.html.jinja:[9]

<!DOCTYPE html>
    <title>{{ variable|escape }}</title>
  {%- for item in item_list %}
    {{ item }}{% if not loop.last %},{% endif %}
  {%- endfor %}

and templating code:

from jinja2 import Template
with open('example.html.jinja') as f:
    tmpl = Template(f.read())
    variable = 'Value with <unsafe> data',
    item_list = [1, 2, 3, 4, 5, 6]

This produces the HTML string:

<!DOCTYPE html>
    <title>Value with &lt;unsafe&gt; data</title>


  1. ^ "Jinja2 Release History". Retrieved 24 June 2020.
  2. ^ "Release 3.1.2".
  3. ^ "Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26.
  4. ^ "Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26.
  5. ^ DuPlain, R. (2013). Instant Flask Web Development. Packt Publishing. p. 30. ISBN 978-1-78216-963-5. Retrieved 2015-05-26.
  6. ^ "Templating (Jinja2) — Ansible Documentation".
  7. ^ "Understanding Jinja". docs.saltproject.io.
  8. ^ "Welcome | Jinja2 (The Python Template Engine)". palletsprojects.com/p/jinja.
  9. ^ Ronacher, Armin. "Template Designer Documentation". Jinja2 Documentation. Retrieved 7 January 2016. A Jinja template doesn't need to have a specific extension: .html, .xml, or any other extension is just fine.

External links[edit]