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 p ...
. It was created by
Armin Ronacher
Armin Ronacher (born 10 May 1989) is an Austrian open source software programmer and the creator of the Flask web framework for Python.
He is a frequent speaker at developer conferences and has a popular blog about software development and open ...
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 lic ...
. 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
Flask may refer to:
Container
* Hip flask, a small container used to carry a small amount of liquid
* Laboratory flask, laboratory glassware for holding larger volumes than simple test tubes
** Erlenmeyer flask, a common laboratory flask wit ...
'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 d ...
,
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. Tra ...
, 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 quantitie ...
.
Features
Some of the features of Jinja are:
* sandboxed execution
* automatic
HTML escaping
In data sanitization, HTML sanitization is the process of examining an HTML document and producing a new HTML document that preserves only whatever tags are designated "safe" and desired. HTML sanitization can be used to protect against attacks s ...
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
SMARTY is a mobile telephone flanker brand operated by Hutchison 3G UK Limited using its Three UK branded mobile network. It aims to attract customers primarily looking for cheaper deals than those available direct from Three, by offering a mo ...
, 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, and ot ...
pipeline
Pipeline may refer to:
Electronics, computers and computing
* Pipeline (computing), a chain of data-processing stages or a CPU optimization found on
** Instruction pipelining, a technique for implementing instruction-level parallelism within a s ...
.
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))
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