1
0
mirror of https://github.com/actix/examples synced 2024-11-23 14:31:07 +01:00
examples/template_yarte
2020-09-12 16:49:45 +01:00
..
src v3 examples (#364) 2020-09-12 16:49:45 +01:00
templates Update yarte version to 0.8 2020-04-28 21:18:18 +02:00
build.rs Update yarte version to 0.8 2020-04-28 21:18:18 +02:00
Cargo.toml v3 examples (#364) 2020-09-12 16:49:45 +01:00
README.md Update yarte version to 0.6.0 and his templates with partial block and html minifier new features (#251) 2020-02-07 02:19:04 +09:00
yarte.toml Update yarte to 0.2 2019-04-01 01:23:31 +02:00

yarte

Minimal example of using template yarte that displays a form.

Template benchmarks in stable

cargo test

cargo run

open localhost:8080

Generated code

impl ::std::fmt::Display for IndexTemplate {
    fn fmt(&self, _fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
        _fmt.write_str(
            "<!DOCTYPE html><html><head><meta charset=\"utf-8\" /><title>Actix \
             web</title></head><body>",
        )?;
        if let Some(name__0) = self.query.get("name") {
            let lastname__1 = self.query.get("lastname").ok_or(yarte::Error)?;
            _fmt.write_str("<h1>Hi, ")?;
            ::yarte::Render::render(&(name__0), _fmt)?;
            _fmt.write_str(" ")?;
            ::yarte::Render::render(&(lastname__1), _fmt)?;
            _fmt.write_str("!</h1><p id=\"hi\" class=\"welcome\">Welcome</p>")?;
        } else {
            _fmt.write_str(
                "<h1 id=\"welcome\" class=\"welcome\">Welcome!</h1><div><h3>What is your \
                 name?</h3><form>Name: <input type=\"text\" name=\"name\" /><br/>Last name: \
                 <input type=\"text\" name=\"lastname\" /><br/><p><input \
                 type=\"submit\"></p></form></div>",
            )?;
        }
        _fmt.write_str("</body></html>")?;
        Ok(())
    }
}

impl ::yarte::Template for IndexTemplate {
    fn mime() -> &'static str {
        "text/html; charset=utf-8"
    }
    
    fn size_hint() -> usize {
        838usize
    }
}

impl ::yarte::aw::Responder for IndexTemplate {
    type Error = ::yarte::aw::Error;
    type Future = ::yarte::aw::Ready<::std::result::Result<::yarte::aw::HttpResponse, Self::Error>>;
    
    #[inline]
    fn respond_to(self, _req: &::yarte::aw::HttpRequest) -> Self::Future {
        match self.call() {
            Ok(body) => ::yarte::aw::ok(
                ::yarte::aw::HttpResponse::Ok()
                    .content_type(Self::mime())
                    .body(body),
            ),
            Err(_) => ::yarte::aw::err(::yarte::aw::ErrorInternalServerError("Some error message")),
        }
    }
}