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

Function index

templating/tera/src/main.rs:15–32  ·  view source on GitHub ↗

store tera template in application state

(
    tmpl: web::Data<tera::Tera>,
    query: web::Query<HashMap<String, String>>,
)

Source from the content-addressed store, hash-verified

13
14// store tera template in application state
15async 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]
35async fn main() -> std::io::Result<()> {

Callers

nothing calls this directly

Calls 3

getMethod · 0.80
insertMethod · 0.80
renderMethod · 0.80

Tested by

no test coverage detected