store tiny_template in application state
(
tmpl: web::Data<TinyTemplate<'_>>,
query: web::Query<HashMap<String, String>>,
)
| 15 | |
| 16 | // store tiny_template in application state |
| 17 | async fn index( |
| 18 | tmpl: web::Data<TinyTemplate<'_>>, |
| 19 | query: web::Query<HashMap<String, String>>, |
| 20 | ) -> Result<HttpResponse, Error> { |
| 21 | let s = if let Some(name) = query.get("name") { |
| 22 | // submitted form |
| 23 | let ctx = json!({ |
| 24 | "name" : name.to_owned(), |
| 25 | "text" : "Welcome!".to_owned() |
| 26 | }); |
| 27 | tmpl.render("user.html", &ctx) |
| 28 | .map_err(|_| error::ErrorInternalServerError("Template error"))? |
| 29 | } else { |
| 30 | tmpl.render("index.html", &serde_json::Value::Null) |
| 31 | .map_err(|_| error::ErrorInternalServerError("Template error"))? |
| 32 | }; |
| 33 | Ok(HttpResponse::Ok().content_type("text/html").body(s)) |
| 34 | } |
| 35 | |
| 36 | #[actix_web::main] |
| 37 | async fn main() -> std::io::Result<()> { |