chore: Add askama to axum for HTML templating

This commit is contained in:
Florian Briand 2024-07-03 16:51:44 +02:00
parent 677740c28c
commit 4bf97f0b95
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B
3 changed files with 15 additions and 1 deletions

View File

@ -4,5 +4,7 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
askama = "0.12.1"
askama_axum = "0.4.0"
axum = "0.7.5" axum = "0.7.5"
tokio = { version = "1.38.0", features = ["full"] } tokio = { version = "1.38.0", features = ["full"] }

View File

@ -2,12 +2,23 @@ use axum::{
routing::get, routing::get,
Router, Router,
}; };
use askama_axum::Template;
#[derive(Template)]
#[template(path = "hello.html")]
struct HelloTemplate<'a> {
name: &'a str,
}
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let app = Router::new() let app = Router::new()
.route("/", get(|| async { "Hello, World!" })); .route("/", get(hello_world));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap(); let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap(); axum::serve(listener, app).await.unwrap();
} }
async fn hello_world() -> HelloTemplate<'static> {
HelloTemplate { name: "world" }
}

1
templates/hello.html Normal file
View File

@ -0,0 +1 @@
Hello, {{ name }}!