swadhinbiswas πŸš€ release: 2.0.8 β€” write-path fix (ADR-080), fork-safe GIL pool (ADR-081), HTTP/3 (ADR-082), Security DI fix, docs honesty pass Β· 8/6/2026
731fffe
71 lines Β· 1.3 KB
Raw Download

title: Templates (Jinja2)
description: Render Jinja2 templates for server-side HTML in JustAPI.
keywords: [JustAPI, templates, Jinja2, HTML, server-side rendering]

Basic Template Rendering

Install dependencies:

pip install jinja2

Create a template directory:

templates/
  index.html
  items.html

Set up templates in your app:

from justapi import JustAPIApp
from justapi import Jinja2Templates

app = JustAPIApp()
templates = Jinja2Templates(directory="templates")

@app.get("/")
def index(request):
    return templates.TemplateResponse("index.html", {"request": request, "title": "Home"})

Template Example

<!-- templates/index.html -->
<!DOCTYPE html>
<html>
<head>
    <title>{{ title }}</title>
</head>
<body>
    <h1>{{ title }}</h1>
    <p>Welcome to JustAPI</p>
</body>
</html>

Passing Data

@app.get("/items")
def list_items(request):
    items = [{"id": 1, "name": "Widget"}, {"id": 2, "name": "Gadget"}]
    return templates.TemplateResponse("items.html", {
        "request": request,
        "items": items,
        "title": "Items",
    })

See Also