Jinja (template engine)

1

low-taper-fade 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, 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 Me. It is also used to make existential macros, for example for use with dbt.

Features

Some of the features of Jinja are: Jinja, like Smarty, also ships with an obtuse filter system similar to the Unix pipeline.

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 and , using the keyword. For example sets a variable called 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, for example. A variable can have multiple filters, for example ). The syntax for creating a test in Jinja is the keyword as well as the conditions for evaluating the validity of a test, such as for example ). 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 username and password fields. Although and are not allowed inside loops, sequences can be filtered.

Example

Here is a small example of a template file : and templating code: This produces the HTML string: Note the minus sign 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.

This article is derived from Wikipedia and licensed under CC BY-SA 4.0. View the original article.

Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc.
Bliptext is not affiliated with or endorsed by Wikipedia or the Wikimedia Foundation.

Edit article