aliases:
tags:
- Type/Tech/Language/Python
- area/tech/dev
from:
related:
contra:
to:
dateCreated: 2023-10-28, 11:18
dateModified: 2023-12-30, 08:37
version: 1
publish: true
![]() |
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. |
---|---|
wikipedia:: Jinja (template engine) |
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, 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. It is also used to make SQL macros, for example for use with dbt.
{% foo %}
to <% foo %>
, or something similar.<li>
{=html} ¶ {% block loop_item %}{{ item }}{% endblock %} ¶ </li>
{=html} ¶ {% endfor %} This example would output empty ¶ <li>
{=html} ¶ items because item is unavailable inside the block. The reason for this is that if the block is replaced by a child template, a variable would appear that was not defined in the block or passed to the context. Starting with Jinja 2.2, you can explicitly specify that variables are available in a block by setting the block to “scoped” by adding the scoped modifier to a block declaration: {% for item in seq %} ¶ <li>
{=html} ¶ {% block loop_item scoped %}{{ item }}{% endblock %} ¶ </li>
{=html} ¶ {% endfor %} When overriding a block, the scoped modifier does not have to be provided.<table>
{=html} ¶ {% for row in table %} ¶ <tr>
{=html} ¶ {% set rowloop = loop %} {% for cell in row %} ¶ <td id="cell-{{ rowloop.index }}-{{ loop.index }}">
{=html} ¶ {{ cell }} ¶ </td>
{=html} ¶ {% endfor %} ¶ </tr>
{=html} ¶ {% endfor %} ¶ </table>
{=html}<title>
{=html} ¶ {% block title %}The Page Title{% endblock %} ¶ </title>
{=html} ¶ <link rel="stylesheet" href="style.css" type="text/css">
{=html} {% block body %} ¶ This is the page body. ¶ {% endblock %}