Claude Code for Jinja2: Python Template Engine — Claude Skills 360 Blog
Blog / AI / Claude Code for Jinja2: Python Template Engine
AI

Claude Code for Jinja2: Python Template Engine

Published: February 9, 2028
Read time: 5 min read
By: Claude Skills 360

Jinja2 is a Python template engine. pip install jinja2. String: from jinja2 import Template; t = Template("Hello {{ name }}!"); t.render(name="Alice"). Env: from jinja2 import Environment, FileSystemLoader. env = Environment(loader=FileSystemLoader("templates/")). tmpl = env.get_template("email.html"). output = tmpl.render(user=user). Loop: {% for item in items %}{{ item }}{% endfor %}. If: {% if user.admin %}Admin{% else %}User{% endif %}. Filter: {{ name|upper }}, {{ price|round(2) }}, {{ items|join(", ") }}, {{ value|default("N/A") }}. Custom filter: env.filters["humanize"] = my_fn. Test: {% if x is defined %}. Macro: {% macro input(name, type="text") %}<input name="{{ name }}">{% endmacro %}. Inheritance: {% extends "base.html" %}{% block content %}...{% endblock %}. Include: {% include "partials/nav.html" %}. Globals: env.globals["site_name"] = "MyApp". Autoescape: Environment(autoescape=True) — escapes <>&"'. Strict: Environment(undefined=StrictUndefined) — raises on missing vars. PackageLoader: PackageLoader("mypackage", "templates"). DictLoader: DictLoader({"t.html": "Hello {{ name }}"}). Sandbox: from jinja2.sandbox import SandboxedEnvironment. Comment: {# comment #}. Raw: {% raw %}{{ not interpolated }}{% endraw %}. Strip whitespace: {%- for x in xs -%}. set: {% set total = items|sum %}. Claude Code generates Jinja2 environments, template hierarchies, and email rendering pipelines.

CLAUDE.md for Jinja2

## Jinja2 Stack
- Version: jinja2 >= 3.1 | pip install jinja2
- Env: Environment(loader=FileSystemLoader("templates/"), autoescape=True)
- Render: env.get_template("name.html").render(**context)
- Loop: {% for x in xs %} | Filter: {{ val|default("N/A")|upper }}
- Inherit: {% extends "base.html" %} {% block name %}...{% endblock %}
- Macro: {% macro name(args) %}...{% endmacro %} — reusable partials
- Strict: undefined=StrictUndefined — raises UndefinedError on missing vars

Jinja2 Template Rendering Pipeline

# app/templates_engine.py — Jinja2 environment, filters, macros, and email rendering
from __future__ import annotations

import html
from pathlib import Path
from typing import Any

from jinja2 import (
    BaseLoader,
    DictLoader,
    Environment,
    FileSystemLoader,
    PackageLoader,
    StrictUndefined,
    Template,
    TemplateNotFound,
    select_autoescape,
)


# ─────────────────────────────────────────────────────────────────────────────
# 1. Environment factory
# ─────────────────────────────────────────────────────────────────────────────

def make_env(template_dir: str | Path | None = None) -> Environment:
    """
    FileSystemLoader looks for templates in template_dir.
    autoescape=True escapes HTML in {{ variables }} — essential for web output.
    StrictUndefined raises UndefinedError on {{ missing_var }} — catches typos early.
    """
    loader: BaseLoader
    if template_dir:
        loader = FileSystemLoader(str(template_dir))
    else:
        loader = DictLoader({})   # replaced below by register_templates()

    env = Environment(
        loader=loader,
        autoescape=select_autoescape(["html", "htm", "xml"]),
        undefined=StrictUndefined,
        trim_blocks=True,     # strip newline after block tags
        lstrip_blocks=True,   # strip leading whitespace before block tags
    )
    _register_filters(env)
    _register_globals(env)
    return env


def _register_filters(env: Environment) -> None:
    """Register custom Jinja2 filters."""
    import humanize as _h

    env.filters["naturalsize"]  = _h.naturalsize
    env.filters["naturaltime"]  = _h.naturaltime
    env.filters["intcomma"]     = _h.intcomma
    env.filters["ordinal"]      = _h.ordinal

    env.filters["currency"] = lambda v, symbol="$": f"{symbol}{v:,.2f}"
    env.filters["pct"]      = lambda v: f"{v:.1%}"
    env.filters["slug"]     = lambda s: s.lower().replace(" ", "-")
    env.filters["truncate_words"] = lambda s, n=20: " ".join(s.split()[:n]) + ("…" if len(s.split()) > n else "")


def _register_globals(env: Environment) -> None:
    """Add global variables available in all templates."""
    from datetime import datetime, timezone
    env.globals["now"]       = lambda: datetime.now(timezone.utc)
    env.globals["site_name"] = "My App"
    env.globals["year"]      = datetime.now().year


# ─────────────────────────────────────────────────────────────────────────────
# 2. In-memory templates (no files needed)
# ─────────────────────────────────────────────────────────────────────────────

TEMPLATES: dict[str, str] = {
    "base.html": """\
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>{% block title %}{{ site_name }}{% endblock %}</title>
</head>
<body>
  {% block content %}{% endblock %}
  <footer>&copy; {{ year }} {{ site_name }}</footer>
</body>
</html>""",

    "email/welcome.html": """\
Hello {{ user.first_name }},

Welcome to {{ site_name }}!

{% if user.plan == "pro" %}
You're on the Pro plan. Here's what you get:
{% for feature in features %}
  - {{ feature }}
{% endfor %}
{% else %}
Start with our free tier and upgrade anytime.
{% endif %}

Your account was created {{ user.created_at|naturaltime }}.

Thanks,
The {{ site_name }} Team""",

    "report.html": """\
{% extends "base.html" %}
{% block title %}{{ title }} — {{ site_name }}{% endblock %}
{% block content %}
<h1>{{ title }}</h1>
<p>Generated {{ now()|naturaltime }}</p>

<table>
  <thead>
    <tr>
      {% for col in columns %}<th>{{ col }}</th>{% endfor %}
    </tr>
  </thead>
  <tbody>
    {% for row in rows %}
    <tr>
      {% for col in columns %}
      <td>{{ row[col]|default("—") }}</td>
      {% endfor %}
    </tr>
    {% endfor %}
  </tbody>
</table>

<p>Total: {{ rows|length }} row(s)</p>
{% endblock %}""",

    "invoice.txt": """\
INVOICE #{{ invoice.number }}
Date: {{ invoice.date }}
Due:  {{ invoice.due_date }}

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

{% for item in items %}
{{ item.description|truncate_words(8) }}: {{ item.amount|currency }}
{% endfor %}
──────────────────────────────────
Subtotal:  {{ subtotal|currency }}
Tax ({{ tax_rate|pct }}): {{ tax_amount|currency }}
TOTAL:     {{ total|currency }}

Thank you for your business!""",
}


def make_dict_env() -> Environment:
    """Environment backed by the in-memory TEMPLATES dict."""
    env = Environment(
        loader=DictLoader(TEMPLATES),
        autoescape=select_autoescape(["html"]),
        undefined=StrictUndefined,
        trim_blocks=True,
        lstrip_blocks=True,
    )
    _register_filters(env)
    _register_globals(env)
    return env


# ─────────────────────────────────────────────────────────────────────────────
# 3. Email renderer
# ─────────────────────────────────────────────────────────────────────────────

def render_welcome_email(user: dict) -> str:
    """Render a welcome email from the in-memory template."""
    env = make_dict_env()
    tmpl = env.get_template("email/welcome.html")
    return tmpl.render(
        user=user,
        features=["Unlimited projects", "Priority support", "Advanced analytics"],
    )


def render_invoice(invoice: dict, customer: dict, items: list[dict]) -> str:
    """Render a plain-text invoice."""
    env = make_dict_env()
    env.autoescape = False   # plain text — no escaping needed

    subtotal   = sum(i["amount"] for i in items)
    tax_rate   = 0.10
    tax_amount = subtotal * tax_rate
    total      = subtotal + tax_amount

    return env.get_template("invoice.txt").render(
        invoice=invoice,
        customer=customer,
        items=items,
        subtotal=subtotal,
        tax_rate=tax_rate,
        tax_amount=tax_amount,
        total=total,
    )


# ─────────────────────────────────────────────────────────────────────────────
# 4. Report builder
# ─────────────────────────────────────────────────────────────────────────────

def render_report(title: str, columns: list[str], rows: list[dict]) -> str:
    """Render an HTML report using base.html inheritance."""
    env = make_dict_env()
    return env.get_template("report.html").render(
        title=title,
        columns=columns,
        rows=rows,
    )


# ─────────────────────────────────────────────────────────────────────────────
# 5. FastAPI integration
# ─────────────────────────────────────────────────────────────────────────────

try:
    from fastapi import FastAPI, Request
    from fastapi.responses import HTMLResponse
    from fastapi.templating import Jinja2Templates

    # Jinja2Templates wraps FileSystemLoader — used by the Starlette integration
    # templates = Jinja2Templates(directory="templates/")

    # Custom: use our env with filters
    class CustomTemplates:
        def __init__(self, directory: str) -> None:
            self.env = make_env(directory)

        def TemplateResponse(self, name: str, context: dict) -> HTMLResponse:
            tmpl = self.env.get_template(name)
            return HTMLResponse(tmpl.render(**context))

    mini_app = FastAPI()

    @mini_app.get("/report", response_class=HTMLResponse)
    async def report_page(request: Request) -> HTMLResponse:
        rows = [{"Product": f"Widget {i}", "Sales": i * 100} for i in range(5)]
        html_content = render_report("Sales Report", ["Product", "Sales"], rows)
        return HTMLResponse(html_content)

except ImportError:
    pass


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    from datetime import datetime, timedelta, timezone

    print("=== Welcome email ===")
    user = {
        "first_name": "Alice",
        "plan": "pro",
        "created_at": datetime.now(timezone.utc) - timedelta(minutes=2),
    }
    email = render_welcome_email(user)
    print(email)

    print("\n=== Invoice ===")
    invoice = render_invoice(
        invoice={"number": "INV-0042", "date": "2024-01-15", "due_date": "2024-02-15"},
        customer={"name": "Acme Corp", "address": "123 Main St, Springfield"},
        items=[
            {"description": "Monthly subscription",            "amount": 99.00},
            {"description": "Additional seats (5 × $10)",      "amount": 50.00},
            {"description": "Professional support add-on",     "amount": 29.99},
        ],
    )
    print(invoice)

    print("\n=== HTML report (truncated) ===")
    report = render_report(
        title="Product Performance",
        columns=["Product", "Revenue", "Units"],
        rows=[
            {"Product": "Widget A", "Revenue": "$1,234", "Units": 42},
            {"Product": "Gadget B", "Revenue": "$5,678", "Units": 100},
        ],
    )
    print(report[:400] + "…")

For the str.format() / f-string alternative — f-strings are excellent for short inline formatting but don’t support loops, conditionals, template inheritance, or reusable macros in external files, making them impractical for multi-block HTML pages or parameterized email templates, while Jinja2’s {% for %} / {% if %} / {% extends %} / {% macro %} tags compose into full page templates where {% block content %} is overridden per page, {% include "nav.html" %} reuses partials, and autoescape=True automatically escapes &, <, > in {{ user_input }} preventing XSS — none of which is possible with f-strings. For the Django templates alternative — Django’s built-in template language uses the same {{ variable }} and {% tag %} syntax but does not allow calling Python functions in templates, while Jinja2’s Environment(undefined=StrictUndefined) raises immediately on missing variables in development, custom env.filters["fn"] = fn registers arbitrary Python callables as template filters in one line, and Jinja2 compiles templates to Python bytecode so repeated tmpl.render() calls are faster than Django’s interpreted template engine. The Claude Skills 360 bundle includes Jinja2 skill sets covering Environment with FileSystemLoader and DictLoader, autoescape with select_autoescape, StrictUndefined for strict mode, custom filter registration, global variable injection, template inheritance with extends/block, include for partials, macro for reusable fragments, trim_blocks and lstrip_blocks for clean whitespace, email and invoice rendering patterns, and FastAPI Jinja2Templates integration. Start with the free tier to try template engine code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free