MCPcopy Create free account
hub / github.com/actix/examples / index

Function index

templating/tinytemplate/src/main.rs:17–34  ·  view source on GitHub ↗

store tiny_template in application state

(
    tmpl: web::Data<TinyTemplate<'_>>,
    query: web::Query<HashMap<String, String>>,
)

Source from the content-addressed store, hash-verified

15
16// store tiny_template in application state
17async 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]
37async fn main() -> std::io::Result<()> {

Callers

nothing calls this directly

Calls 2

getMethod · 0.80
renderMethod · 0.80

Tested by

no test coverage detected