3.9. Jinja#

Jinja is a fast and flexible templating engine for Python, commonly used to dynamically generate HTML, configuration files, or other text-based formats. It allows embedding Python-like expressions and control structures within templates using a concise syntax, such as {{ }} for variable interpolation and {% %} for logic statements like loops and conditionals. Jinja supports features like template inheritance, macros, filters, and extensions, enabling reusable and modular templates. Widely adopted in web frameworks like Flask and for tools like Ansible, Jinja simplifies rendering dynamic content in various contexts.

3.9.1. Variables#

Jinja templates can access variables passed into the template and render them dynamically. For example, a template Hello, {{ name }}! with the variable name set to "Alice" will render as Hello, Alice!.

3.9.2. Filters#

Filters are used to transform data in templates. Common Filters include:

  • upper: Converts a string to uppercase.

  • lower: Converts a string to lowercase.

  • length: Returns the length of a collection.

  • default: Sets a default value if the variable is undefined.

Filters are applied using a pipe (|). For example, a template {{ name|upper }} with the variable name set to "Alice" will render as ALICE.

3.9.3. Loops#

Use {% for %} to iterate over items:

{% for item in items %}
- {{ item }}
{% endfor %}

Output:

- apple
- banana
- cherry

3.9.4. Conditionals#

Use {% if %}, {% elif %}, and {% else %} for conditional logic.

{% if user.is_admin %}
Welcome, admin!
{% else %}
Welcome, user!
{% endif %}

3.9.5. Macros#

Macros are reusable blocks of code within templates.

{% macro greet(name) %}
Hello, {{ name }}!
{% endmacro %}

{{ greet("Alice") }}

3.9.6. Resources#