MVC (Templates)
Austial is mostly used for APIs, but a handler can render an HTML page with Jinja2. Point the app at a views folder and mark the
handler with @Render; the dict it returns becomes the template context:
from austial import Controller, Get, Query, Render
@Controller()
class PagesController:
@Get()
@Render("index.html")
async def home(self, name: str = Query("name", default="world")):
return {"name": name, "items": ["a", "b"]}<!-- views/index.html -->
<h1>Hello {{ name }}</h1>
<ul>{% for item in items %}<li>{{ item }}</li>{% endfor %}</ul>app = AustialFactory.create(AppModule)
app.set_base_views_dir("views")set_base_views_dirmust be called before the application is initialised. Using@Renderwithout it fails the request with a 500 and logs why.- The response is
text/htmlwith the route’s usual status code (@HttpCodeworks). - The handler’s return value must be a dict (anything else renders with an empty context). Guards, pipes and interceptors run as for any route.
- Serve CSS, JS and images with
use_static_assets. - Autoescaping is Jinja2’s default for
.htmltemplates; still never put untrusted input in a|safefilter.