Jinja (template engine)

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search
Original author(s) Armin Ronacher
Stable release
2.10 / November 8, 2017; 7 months ago (2017-11-08)[1]
  • https://github.com/pallets/jinja
Written in Python
Type Template engine
License BSD License
Website jinja.pocoo.org

Jinja is a template engine for the Python programming language and is licensed under a BSD License created by Armin Ronacher. It 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 sourcecode.

The Jinja template engine allows customization of tags,[2] filters, tests, and globals.[3] 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.[4]


Some of the features of Jinja are:[5]

  • sandboxed execution
  • powerful 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. 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'[6]

<!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())
print tmpl.render(
    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. ^ https://github.com/pallets/jinja/releases
  2. ^ "Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26. 
  3. ^ "Extensions". Jinja2 Documentation (2.8-dev). Retrieved 2015-05-26. 
  4. ^ DuPlain, R. (2013). Instant Flask Web Development. Packt Publishing. p. 30. ISBN 978-1-78216-963-5. Retrieved 2015-05-26. 
  5. ^ http://jinja.pocoo.org/
  6. ^ 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

  • Jinja website
  • Jinja2 on Python pypi
  • jinja2 on GitHub
Retrieved from "https://en.wikipedia.org/w/index.php?title=Jinja_(template_engine)&oldid=843555101"
This content was retrieved from Wikipedia : http://en.wikipedia.org/wiki/Jinja_(template_engine)
This page is based on the copyrighted Wikipedia article "Jinja (template engine)"; it is used under the Creative Commons Attribution-ShareAlike 3.0 Unported License (CC-BY-SA). You may redistribute it, verbatim or modified, providing that you comply with the terms of the CC-BY-SA