Jinja (template Engine)
   HOME

TheInfoList



OR:

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 A sandbox is a sandpit, a wide, shallow playground construction to hold sand, often made of wood or plastic. Sandbox or sand box may also refer to: Arts, entertainment, and media * Sandbox (band), a Canadian rock music group * Sandbox (Gu ...
. 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 (for formatting or transforming values), tests (for evaluating conditions), 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, Trac, and
Salt In common usage, salt is a mineral composed primarily of sodium chloride (NaCl). When used in food, especially in granulated form, it is more formally called table salt. In the form of a natural crystalline mineral, salt is also known as r ...
. It is also used to make SQL macros, for example for use with dbt.


Features

Some of the features of Jinja are: * 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 Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user 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 A pipeline is a system of Pipe (fluid conveyance), pipes for long-distance transportation of a liquid or gas, typically to a market area for consumption. The latest data from 2014 gives a total of slightly less than of pipeline in 120 countries ...
.


Syntax

The syntax for printing output in Jinja is using the double curly braces, for example . Statements which set variables in jinja or those which do not have an output can be wrapped within , 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, . The syntax for creating a filter in Jinja is a
vertical bar The vertical bar, , is a glyph with various uses in mathematics, computing, and typography. It has many names, often related to particular meanings: Sheffer stroke (in logic), pipe, bar, or (literally, the word "or"), vbar, and others. Usage ...
(, ), for example . A variable can have multiple filters, for example ). 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: , 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 Note the
minus sign The plus sign () and the minus sign () are mathematical symbols used to denote positive and negative functions, respectively. In addition, the symbol represents the operation of addition, which results in a sum, while the symbol represent ...
(-) 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.


References


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