mirror of
https://github.com/actix/examples
synced 2025-02-20 08:20:32 +01:00
Update yarte version to 0.6.0 and his templates with partial block and html minifier new features (#251)
This commit is contained in:
parent
443b8197ed
commit
7198fa31e9
@ -10,10 +10,10 @@ workspace = ".."
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
env_logger = "0.7"
|
env_logger = "0.7"
|
||||||
|
|
||||||
yarte = { version = "0.5", features = ["with-actix-web"] }
|
yarte = { version = "0.6", features = ["with-actix-web"] }
|
||||||
|
|
||||||
actix-rt = "1.0"
|
actix-rt = "1.0"
|
||||||
actix-web = "2.0.0"
|
actix-web = "2.0.0"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
yarte = { version = "0.5", features = ["with-actix-web"] }
|
yarte = { version = "0.6", features = ["with-actix-web"] }
|
||||||
|
@ -10,3 +10,59 @@ cargo test
|
|||||||
cargo run
|
cargo run
|
||||||
```
|
```
|
||||||
> open `localhost:8080`
|
> open `localhost:8080`
|
||||||
|
|
||||||
|
## Generated code
|
||||||
|
```rust
|
||||||
|
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")),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
8
template_yarte/templates/base.hbs
Normal file
8
template_yarte/templates/base.hbs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{{! Simple example !}}
|
||||||
|
{{> doc/t }}
|
||||||
|
<html>
|
||||||
|
{{> doc/head }}
|
||||||
|
<body>
|
||||||
|
{{> @partial-block }}
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,10 +0,0 @@
|
|||||||
<body>
|
|
||||||
|
|
||||||
{{~#if let Some(name) = query.get("name") }}
|
|
||||||
{{ let lastname = query.get("lastname").ok_or(yarte::Error)? }}
|
|
||||||
{{> card/hi ~}}
|
|
||||||
{{ else ~}}
|
|
||||||
{{> card/form ~}}
|
|
||||||
{{/if ~}}
|
|
||||||
|
|
||||||
</body>
|
|
@ -9,12 +9,11 @@
|
|||||||
{{! Form !}}
|
{{! Form !}}
|
||||||
<form>
|
<form>
|
||||||
{{! Input name !}}
|
{{! Input name !}}
|
||||||
Name: <input type="text" name="name" />
|
Name: <input type="text" name="name" /><br/>
|
||||||
{{! Input last name !}}
|
{{! Input last name !}}
|
||||||
<br/>Last name: <input type="text" name="lastname" />
|
Last name: <input type="text" name="lastname"/><br/>
|
||||||
|
<p>
|
||||||
{{! Submit !}}
|
<input type="submit">
|
||||||
<br/><p><input type="submit"></p></form>
|
</p>
|
||||||
|
</form>
|
||||||
{{! Order and remove whitespace with comments !}}
|
|
||||||
</div>
|
</div>
|
||||||
|
@ -1 +1,4 @@
|
|||||||
<head><meta charset="utf-8" /><title>{{ title }}</title></head>
|
<head>
|
||||||
|
<meta charset="utf-8"/>
|
||||||
|
<title>{{ title }}</title>
|
||||||
|
</head>
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
{{! Simple example !}}
|
{{#> base title = "Actix web" }}
|
||||||
{{> doc/t ~}}
|
{{~#if let Some(name) = query.get("name") }}
|
||||||
<html>
|
{{ let lastname = query.get("lastname").ok_or(yarte::Error)? }}
|
||||||
|
{{> card/hi ~}}
|
||||||
{{~> doc/head title = "Actix web" }}
|
{{ else ~}}
|
||||||
{{~> body ~}}
|
{{> card/form ~}}
|
||||||
|
{{/if ~}}
|
||||||
</html>
|
{{/base }}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user