From 4bf97f0b95e6e51ddb3d8b66ee304f1b6f2243b0 Mon Sep 17 00:00:00 2001 From: Florian Briand Date: Wed, 3 Jul 2024 16:51:44 +0200 Subject: [PATCH] chore: Add askama to axum for HTML templating --- Cargo.toml | 2 ++ src/main.rs | 13 ++++++++++++- templates/hello.html | 1 + 3 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 templates/hello.html diff --git a/Cargo.toml b/Cargo.toml index a4443e2..e4538ae 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,5 +4,7 @@ version = "0.1.0" edition = "2021" [dependencies] +askama = "0.12.1" +askama_axum = "0.4.0" axum = "0.7.5" tokio = { version = "1.38.0", features = ["full"] } diff --git a/src/main.rs b/src/main.rs index 740b768..872e94e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,23 @@ use axum::{ routing::get, Router, }; +use askama_axum::Template; + +#[derive(Template)] +#[template(path = "hello.html")] +struct HelloTemplate<'a> { + name: &'a str, +} #[tokio::main] async fn main() { 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(); axum::serve(listener, app).await.unwrap(); } + +async fn hello_world() -> HelloTemplate<'static> { + HelloTemplate { name: "world" } +} diff --git a/templates/hello.html b/templates/hello.html new file mode 100644 index 0000000..8149be7 --- /dev/null +++ b/templates/hello.html @@ -0,0 +1 @@ +Hello, {{ name }}!