Can you provide more Jinja2 examples?

QuestionsQuestions8 SkillsProAnsible Jinja2 TemplatesOct, 28 2025
095

Certainly! Here are several Jinja2 examples that demonstrate different features and use cases:

1. Basic Variable Rendering

This example shows how to render a simple variable in a template.

Template (greeting.j2):

Hello, {{ user_name }}!

Python Code:

from jinja2 import Template

template = Template("Hello, {{ user_name }}!")
rendered = template.render(user_name="Alice")
print(rendered)  # Output: Hello, Alice!

2. Using Filters

Filters modify the output of variables. Here’s an example using the upper filter.

Template (message.j2):

Your name in uppercase: {{ user_name | upper }}

Python Code:

template = Template("Your name in uppercase: {{ user_name | upper }}")
rendered = template.render(user_name="Alice")
print(rendered)  # Output: Your name in uppercase: ALICE

3. Control Structures: If Statement

You can use control structures like if statements to conditionally render content.

Template (status.j2):

{% if is_active %}
    User is active.
{% else %}
    User is inactive.
{% endif %}

Python Code:

template = Template("{% if is_active %}User is active.{% else %}User is inactive.{% endif %}")
rendered = template.render(is_active=True)
print(rendered)  # Output: User is active.

4. Control Structures: For Loop

You can loop through lists or dictionaries using the for statement.

Template (items.j2):

Items in your cart:
<ul>
{% for item in items %}
    <li>{{ item }}</li>
{% endfor %}
</ul>

Python Code:

template = Template("""
Items in your cart:
<ul>
{% for item in items %}
    <li>{{ item }}</li>
{% endfor %}
</ul>
""")
rendered = template.render(items=["Apple", "Banana", "Cherry"])
print(rendered)

5. Template Inheritance

You can create a base template and extend it in child templates.

Base Template (base.j2):

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
    </header>
    <main>
        {% block content %}{% endblock %}
    </main>
</body>
</html>

Child Template (home.j2):

{% extends "base.j2" %}

{% block title %}Home{% endblock %}

{% block content %}
    <p>This is the home page.</p>
{% endblock %}

6. Using Macros

Macros allow you to define reusable snippets of code.

Template (macros.j2):

{% macro render_item(item) %}
    <li>{{ item }}</li>
{% endmacro %}

<ul>
{% for item in items %}
    {{ render_item(item) }}
{% endfor %}
</ul>

Python Code:

template = Template("""
{% macro render_item(item) %}
    <li>{{ item }}</li>
{% endmacro %}

<ul>
{% for item in items %}
    {{ render_item(item) }}
{% endfor %}
</ul>
""")
rendered = template.render(items=["Apple", "Banana", "Cherry"])
print(rendered)

Further Learning

These examples illustrate the versatility of Jinja2 in various contexts. To explore more, consider:

  • Jinja2 Documentation: For in-depth details on syntax and features.
  • Flask or Django Tutorials: To see Jinja2 in action within web applications.

If you have any specific scenarios in mind or need more examples, feel free to ask!

0 Comments

no data
Be the first to share your comment!