Skip to content

Jinja2 Templates

Fracttal ETL Hub uses Jinja2 templates to generate dynamic content during transformations. Templates allow creating documents, emails, and personalized content based on input data.

Basic Syntax

Variables

Hello {{ name }} {{ lastname }}

Your age is: {{ age }}

Filters

{{ name | upper }}
{{ price | round(2) }}
{{ date | strftime('%d/%m/%Y') }}

Conditionals

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

Loops

{% for item in items %}
    - {{ item.name }}: {{ item.price }}
{% endfor %}

Common Filters

String Filters

{{ text | upper }}           # UPPERCASE
{{ text | lower }}           # lowercase
{{ text | title }}           # Title Case
{{ text | trim }}            # Remove whitespace
{{ text | replace('old', 'new') }}  # Replace text

Number Filters

{{ number | round(2) }}      # Round to 2 decimals
{{ number | abs }}           # Absolute value
{{ price | currency }}       # Format as currency

Date Filters

{{ date | strftime('%Y-%m-%d') }}    # Format date
{{ date | age }}                     # Calculate age
{{ now() | strftime('%H:%M') }}      # Current time

List Filters

{{ items | length }}         # Count items
{{ items | first }}          # First item
{{ items | last }}           # Last item
{{ items | join(', ') }}     # Join with separator

Template Examples

Email Template

Subject: Welcome {{ user.name }}!

Dear {{ user.name }},

Thank you for registering with us on {{ registration_date | strftime('%B %d, %Y') }}.

Your account details:
- Username: {{ user.username }}
- Email: {{ user.email }}
- Plan: {{ user.plan | title }}

{% if user.plan == 'premium' %}
You have access to premium features!
{% endif %}

Best regards,
The Team

Invoice Template

INVOICE #{{ invoice.number }}
Date: {{ invoice.date | strftime('%d/%m/%Y') }}

Bill To:
{{ customer.name }}
{{ customer.address }}

Items:
{% for item in invoice.items %}
{{ item.description | ljust(30) }} {{ item.quantity }} x {{ item.price | currency }} = {{ (item.quantity * item.price) | currency }}
{% endfor %}

Subtotal: {{ invoice.subtotal | currency }}
Tax ({{ tax_rate }}%): {{ invoice.tax | currency }}
Total: {{ invoice.total | currency }}

HTML Report Template

<!DOCTYPE html>
<html>
<head>
    <title>Sales Report - {{ report_date | strftime('%B %Y') }}</title>
</head>
<body>
    <h1>Monthly Sales Report</h1>
    <p>Generated on: {{ now() | strftime('%d/%m/%Y %H:%M') }}</p>

    <table>
        <thead>
            <tr>
                <th>Product</th>
                <th>Sales</th>
                <th>Revenue</th>
            </tr>
        </thead>
        <tbody>
            {% for product in products %}
            <tr>
                <td>{{ product.name }}</td>
                <td>{{ product.sales }}</td>
                <td>{{ product.revenue | currency }}</td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

    <p>Total Revenue: {{ total_revenue | currency }}</p>
</body>
</html>

Advanced Features

Macros

{% macro render_field(field) %}
    <div class="field">
        <label>{{ field.label }}</label>
        <input name="{{ field.name }}" value="{{ field.value }}">
    </div>
{% endmacro %}

{{ render_field(name_field) }}
{{ render_field(email_field) }}

Includes

{% include 'header.html' %}
<main>
    {{ content }}
</main>
{% include 'footer.html' %}

Inheritance

{# base.html #}
<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}{% endblock %}</title>
</head>
<body>
    {% block content %}{% endblock %}
</body>
</html>

{# page.html #}
{% extends "base.html" %}

{% block title %}My Page{% endblock %}

{% block content %}
    <h1>Welcome!</h1>
{% endblock %}

Configuration in ETL

Template Transform

{
  "transform": {
    "template": {
      "template_string": "Hello {{ name }}, your order #{{ order_id }} is ready!",
      "variables": {
        "name": {"var": "customer_name"},
        "order_id": {"var": "id"}
      }
    }
  }
}

File Template

{
  "transform": {
    "template": {
      "template_file": "email_template.j2",
      "variables": {
        "user": {"var": "user_data"},
        "date": {"var": "current_date"}
      }
    }
  }
}

Best Practices

  1. Keep templates simple and readable
  2. Use meaningful variable names
  3. Add comments for complex logic
  4. Test templates with sample data
  5. Handle missing variables gracefully
  6. Use filters for data formatting
  7. Organize templates in folders
  8. Version control template files

Security Considerations

  • Sanitize input to prevent injection
  • Validate variables before rendering
  • Escape HTML content when needed
  • Limit template complexity
  • Use safe filters for user content