Jinja (template engine)

Short description: Template engine for the Python programming language associated with the Flask framework

Jinja

Original author(s)| Armin Ronacher
Initial release| July 17, 2008; 17 years ago (2008-07-17)[1]
Written in| Python
Type| Template engine
License| BSD License

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. 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,[2] filters (for formatting or transforming values[3]), tests (for evaluating conditions[3]), and globals.[4] 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 [5] and it is also used by Ansible,[6] Trac, and Salt.[7] It is also used to make SQL macros, for example for use with dbt.[8]

## Contents

* 1 Features
* 2 Syntax
* 3 Example
* 4 Sources
* 5 External links

## Features

Some of the features of Jinja are:[9]

* 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 pipeline.

## Syntax

The syntax for printing output in Jinja is using the double curly braces, for example `{{ Hello, World! }}`.

Statements which set variables in jinja or those which do not have an output can be wrapped within `{%` and `%}`, using the `set` keyword. For example `{% set foo = 42 %}` sets a variable called `foo` with a value of 42.

Similar to above, comments in jinja can be written using hashtag (`#`) instead of a percentage (`%`), for example, `{# helpful comment #}`.

The syntax for creating a filter in Jinja is a vertical bar (`|`), for example `{{ variable|filter }}`. A variable can have multiple filters, for example `{{ variable|filter|filter }}`).[3]

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 `{% if variable is divisibleby 10 %}do something{% endif %}`).[3]

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.

{% for user in users %}
{% endfor %}

Although Template:Python and Template:Python are not allowed inside loops, sequences can be filtered.

## Example

Here is a small example of a template file `example.html.jinja`:[10]

<!DOCTYPE html>
<html>
<head>
<title>{{ variable|escape }}</title>
</head>
<body>
{%- for item in item_list %}
{{ item }}{% if not loop.last %},{% endif %}
{%- endfor %}
</body>
</html>

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>
<html>
<head>
<title>Value with &lt;unsafe&gt; data</title>
</head>
<body>

</body>
</html>

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.[11]

## Sources

1. ↑ "Jinja2 Release History". https://pypi.org/project/Jinja2/#history. Retrieved 24 June 2020.
2. ↑ "Extensions". https://jinja.palletsprojects.com/en/2.11.x/extensions/#module-jinja2.ext. Retrieved 2015-05-26.
3. ↑ 3.0 3.1 3.2 3.3 "Jinja built-in filters and tests (like Django filters)". www.webforefront.com. https://www.webforefront.com/django/usebuiltinjinjafilters.html.
4. ↑ "Extensions". https://jinja.palletsprojects.com/en/2.11.x/extensions/#expression-statement. Retrieved 2015-05-26.
5. ↑ DuPlain, R. (2013). Instant Flask Web Development. Packt Publishing. p. 30. ISBN 978-1-78216-963-5. https://books.google.com/books?id=nbJVx2uDfgoC&pg=PT30. Retrieved 2015-05-26.
6. ↑ "Templating (Jinja2) — Ansible Documentation". https://docs.ansible.com/ansible/latest/user_guide/playbooks_templating.html.
7. ↑ "Understanding Jinja". https://docs.saltproject.io/en/latest/topics/jinja/index.html.
8. ↑ Jinja and macros | dbt Developer Hub
9. ↑ "Welcome | Jinja2 (The Python Template Engine)". https://palletsprojects.com/p/jinja/.
10. ↑ Ronacher, Armin. "Template Designer Documentation". https://jinja.palletsprojects.com/en/3.0.x/templates/. Retrieved 9 January 2024. "A Jinja template doesn’t need to have a specific extension: .html, .xml, or any other extension is just fine."
11. ↑ "Template Designer Documentation — Jinja Documentation (3.0.x)". https://jinja.palletsprojects.com/en/3.0.x/templates/#whitespace-control.

## External links

0.00

(0 votes)

Original source: https://en.wikipedia.org/wiki/Jinja (template engine). Read more

|

Retrieved from "https://handwiki.org/wiki/index.php?title=Software:Jinja_(template_engine)&oldid=457294"