Jinax Templates

Jinax is Flaxon's optional Jinja2 template integration with autoescaping, inheritance, filters, and async support.

Installation

pip install flaxon[templates]

Setup

from flaxon import Flaxon
from flaxon.jinax import Jinax

app = Flaxon("website")
app.use_templates(Jinax("templates", auto_reload=True))

Basic Template

templates/home.html

<!doctype html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>Welcome, {{ name }}!</h1>
    <ul>
    {% for item in items %}
        <li>{{ item }}</li>
    {% endfor %}
    </ul>
</body>
</html>

Route

@app.get("/")
async def home(request):
    return await request.render("home.html", {
        "title": "Home",
        "name": "World",
        "items": ["Routing", "Validation", "Templates"],
    })

Template Inheritance

templates/base.html

<!doctype html>
<html>
<head>
    <title>{% block title %}Flaxon{% endblock %}</title>
</head>
<body>
    <header>{% block header %}Default Header{% endblock %}</header>
    <main>{% block content %}{% endblock %}</main>
    <footer>{% block footer %}Default Footer{% endblock %}</footer>
</body>
</html>

templates/page.html

{% extends "base.html" %}

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

{% block header %}Welcome to My Page{% endblock %}

{% block content %}
    <h1>{{ heading }}</h1>
    <p>{{ description }}</p>
{% endblock %}

Filters

{{ value|capitalize }}
{{ value|lower }}
{{ value|upper }}
{{ value|title }}
{{ value|trim }}
{{ value|escape }}
{{ value|safe }}
{{ value|json }}
{{ value|length }}
{{ value|reverse }}
{{ value|join(", ") }}
{{ value|replace(old, new) }}
{{ value|date("%Y-%m-%d") }}
{{ value|datetime("%Y-%m-%d %H:%M:%S") }}
{{ value|currency("USD") }}
{{ value|truncate(100, "...") }}
{{ value|default("N/A") }}

Custom Filters

jinax = Jinax("templates")

def currency_filter(value, symbol="$"):
    return f"{symbol}{value:.2f}"

jinax.add_filter("currency", currency_filter)

@app.get("/")
async def home(request):
    return await request.render("product.html", {
        "price": 99.99,
    })
Tip

Jinax templates are lazily loaded. If you don't use them, Jinja2 is never imported.