Cargo.toml
[dependencies] axum-template="0.14.0" handlebars="4.3.6"
テンプレートファイルの用意
template/index.hbs
<body class="container"> <h1 class="display-6 my-2">{{title}}</h1> <p class="my-2">{{ message }}</p> </body>
main.rs
async fn handle_index() -> axum::response::Html<String> { let mut params = std::collections::HashMap::new(); params.insert("title", "Index page"); params.insert("message", "This is sample page message!"); let mut handlebars = handlebars::Handlebars::new(); handlebars .register_template_string("hello", include_str!("template/index.hbs")); let template = handlebars.render("hello", ¶ms).unwrap(); axum::response::Html(template) }