Answer#
askama turns each template into Rust code at compile time. The template file:
<h1>Hello {{ name }}</h1>and the handler:
use askama::Template;
use axum::{body::{to_bytes, Body}, extract::Path, http::{Request, StatusCode}, response::Html, routing::get, Router};
use tower::ServiceExt;
#[derive(Template)]
#[template(path = "hello.html")] // templates/hello.html, relative to the crate root
struct Hello {
name: String,
}
async fn hello(Path(name): Path<String>) -> Result<Html<String>, StatusCode> {
Hello { name }.render().map(Html).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/hello/{name}", get(hello));
for uri in ["/hello/Ada", "/hello/%3Cb%3Ebold%3C%2Fb%3E"] {
let res = app.clone().oneshot(Request::get(uri).body(Body::empty()).unwrap()).await.unwrap();
let body = to_bytes(res.into_body(), usize::MAX).await.unwrap();
print!("{}", String::from_utf8_lossy(&body));
}
}<h1>Hello Ada</h1><h1>Hello <b>bold</b></h1>The plain name was printed as is. The name <b>bold</b> came out as <b>bold</b>: askama escapes values in .html templates by default.
A misspelled variable stops the build. With {{ nam }} in the template, cargo check reports:
error[E0609]: no field `nam` on type `&Hello`
--> examples\f24.rs:3:10
|
3 | #[derive(Template)]
| ^^^^^^^^ unknown field
...askama = "0.14.0"Common mistakes#
- Expecting a template edit to show without building: the template is part of the compiled program, so the program must be built again.
- Expecting run-time reloading in production. If non-programmers must change templates without a rebuild, use a run-time engine (see the minijinja thread).
Related questions#
Which one should I pick?#
Compile-time checking (askama) finds mistakes early; run-time loading (minijinja) allows changing templates without compiling. Both escape HTML by default for .html names.
Sources#
- askama documentation: https://docs.rs/askama/0.14.0/askama/ (read 2026-12-05)