store tera template in application state
(
tmpl: web::Data<tera::Tera>,
query: web::Query<HashMap<String, String>>,
)
| 13 | |
| 14 | // store tera template in application state |
| 15 | async fn index( |
| 16 | tmpl: web::Data<tera::Tera>, |
| 17 | query: web::Query<HashMap<String, String>>, |
| 18 | ) -> Result<impl Responder, Error> { |
| 19 | let html = if let Some(name) = query.get("name") { |
| 20 | // submitted form |
| 21 | let mut ctx = tera::Context::new(); |
| 22 | ctx.insert("name", name); |
| 23 | ctx.insert("text", "Welcome!"); |
| 24 | tmpl.render("user.html", &ctx) |
| 25 | .map_err(|_| error::ErrorInternalServerError("Template error"))? |
| 26 | } else { |
| 27 | tmpl.render("index.html", &tera::Context::new()) |
| 28 | .map_err(|_| error::ErrorInternalServerError("Template error"))? |
| 29 | }; |
| 30 | |
| 31 | Ok(web::Html::new(html)) |
| 32 | } |
| 33 | |
| 34 | #[actix_web::main] |
| 35 | async fn main() -> std::io::Result<()> { |