Jinja is a
web template engine for the
Python programming language
Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation.
Python is dynamically-typed and garbage-collected. It supports multiple programming par ...
. It was created by
Armin Ronacher
Armin Ronacher (born 10 May 1989) is an Austrian open source software programmer
A computer programmer, sometimes referred to as a software developer, a software engineer, a programmer or a coder, is a person who creates computer programs � ...
and is licensed under a
BSD License
BSD licenses are a family of permissive free software licenses, imposing minimal restrictions on the use and distribution of covered software. This is in contrast to copyleft licenses, which have share-alike requirements. The original BSD li ...
. 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,
filters, tests, and globals.
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
and it is also used by
Ansible
An ansible is a category of fictional devices or technology capable of near-instantaneous or faster-than-light communication. It can send and receive messages to and from a corresponding device over any distance or obstacle whatsoever with no de ...
,
Trac
Trac is an open-source, web-based project management and bug tracking system. It has been adopted by a variety of organizations for use as a bug tracking system for both free and open-source software and proprietary projects and products. Trac ...
, and
Salt
Salt is a mineral composed primarily of sodium chloride (NaCl), a chemical compound belonging to the larger class of salts; salt in the form of a natural crystalline mineral is known as rock salt or halite. Salt is present in vast quanti ...
.
Features
Some of the features of Jinja are:
* sandboxed execution
* automatic
HTML escaping to prevent
cross-site scripting
Cross-site scripting (XSS) is a type of security vulnerability that can be found in some web applications. XSS attacks enable attackers to inject client-side scripts into web pages viewed by other users. A cross-site scripting vulnerability may ...
(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
Unix (; trademarked as UNIX) is a family of multitasking, multiuser computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
pipeline.
Example
Here is a small example of a template file
example.html.jinja
:
,
and templating code:
from jinja2 import Template
with open('example.html.jinja') as f:
tmpl = Template(f.read())
print(tmpl.render(
variable = 'Value with data',
item_list = , 2, 3, 4, 5, 6
The comma is a punctuation mark that appears in several variants in different languages. It has the same shape as an apostrophe or single closing quotation mark () in many typefaces, but it differs from them in being placed on the baseline ...
))
This produces the HTML string:
Value with <unsafe> data
1,
2,
3,
4,
5,
6
Sources
External links
* {{Official website, https://palletsprojects.com/p/jinja/
Free software programmed in Python
Free system software
Python (programming language) libraries
Python (programming language) software
Template engines
Software using the BSD license
Articles with example Python (programming language) code