1
0
mirror of https://github.com/actix/examples synced 2024-11-23 22:41:07 +01:00

Merge pull request #307 from botika/master

Update yarte version to 0.8
This commit is contained in:
Yuki Okushi 2020-04-29 11:11:57 +09:00 committed by GitHub
commit 53c778d436
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 41 deletions

View File

@ -34,7 +34,8 @@ members = [
"rustls", "rustls",
"shutdown-server", "shutdown-server",
"server-sent-events", "server-sent-events",
"simple-auth-server", # Depency clang outdate bindgen@0.48
# "simple-auth-server",
"state", "state",
"static_index", "static_index",
"template_askama", "template_askama",

View File

@ -10,10 +10,12 @@ workspace = ".."
[dependencies] [dependencies]
env_logger = "0.7" env_logger = "0.7"
yarte = { version = "0.7", features = ["with-actix-web"] } yarte = { version = "0.8", features = ["html-min"] }
actix-rt = "1.0" actix-rt = "1.0"
actix-web = "2.0.0" actix-web = "2.0.0"
[build-dependencies] [build-dependencies.yarte_helpers]
yarte = { version = "0.7", features = ["with-actix-web"] } version = "0.8"
default-features = false
features = ["config"]

View File

@ -1,3 +1,3 @@
fn main() { fn main() {
yarte::recompile::when_changed(); yarte_helpers::recompile::when_changed();
} }

View File

@ -1,17 +1,29 @@
use std::collections::HashMap; use std::collections::HashMap;
use actix_web::{get, middleware::Logger, web, App, HttpServer, Responder}; use actix_web::{
use yarte::Template; error::ErrorInternalServerError, get, middleware::Logger, web, App, Error,
HttpResponse, HttpServer,
};
use yarte::TemplateMin;
#[derive(Template)] #[derive(TemplateMin)]
#[template(path = "index.hbs", err = "Some error message", mode = "html-min")] #[template(path = "index")]
struct IndexTemplate { struct IndexTemplate {
query: web::Query<HashMap<String, String>>, query: web::Query<HashMap<String, String>>,
} }
#[get("/")] #[get("/")]
async fn index(query: web::Query<HashMap<String, String>>) -> impl Responder { async fn index(
query: web::Query<HashMap<String, String>>,
) -> Result<HttpResponse, Error> {
IndexTemplate { query } IndexTemplate { query }
.call()
.map(|body| {
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(body)
})
.map_err(|_| ErrorInternalServerError("Some error message"))
} }
#[actix_rt::main] #[actix_rt::main]
@ -49,17 +61,11 @@ mod test {
assert_eq!( assert_eq!(
bytes, bytes,
Bytes::from_static( Bytes::from_static(
"<!DOCTYPE html>\ "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
<html>\ web</title></head><body><h1 id=\"welcome\" \
<head><meta charset=\"utf-8\"><title>Actix web</title></head><body>\ class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
<h1 id=\"welcome\" class=\"welcome\">Welcome!</h1><div>\ <input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
<h3>What is your name?</h3>\ name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
<form>\
Name: <input type=\"text\" name=\"name\">\
<br>Last name: <input type=\"text\" name=\"lastname\">\
<br><p><input type=\"submit\"></p></form>\
</div>\
</body></html>"
.as_ref() .as_ref()
) )
); );
@ -78,12 +84,9 @@ mod test {
assert_eq!( assert_eq!(
bytes, bytes,
Bytes::from_static( Bytes::from_static(
"<!DOCTYPE html>\ "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
<html>\ web</title></head><body><h1>Hi, foo bar!</h1><p id=\"hi\" \
<head><meta charset=\"utf-8\"><title>Actix web</title></head>\ class=\"welcome\">Welcome</p></body></html>"
<body>\
<h1>Hi, foo bar!</h1><p id=\"hi\" class=\"welcome\">Welcome</p>\
</body></html>"
.as_ref() .as_ref()
) )
); );
@ -111,17 +114,11 @@ mod test {
assert_eq!( assert_eq!(
bytes, bytes,
Bytes::from_static( Bytes::from_static(
"<!DOCTYPE html>\ "<!DOCTYPE html><html><head><meta charset=\"utf-8\"><title>Actix \
<html>\ web</title></head><body><h1 id=\"welcome\" \
<head><meta charset=\"utf-8\"><title>Actix web</title></head><body>\ class=\"welcome\">Welcome!</h1><div><h3>What is your name?</h3><form>Name: \
<h1 id=\"welcome\" class=\"welcome\">Welcome!</h1><div>\ <input type=\"text\" name=\"name\"><br>Last name: <input type=\"text\" \
<h3>What is your name?</h3>\ name=\"lastname\"><br><p><input type=\"submit\"></p></form></div></body></html>"
<form>\
Name: <input type=\"text\" name=\"name\">\
<br>Last name: <input type=\"text\" name=\"lastname\">\
<br><p><input type=\"submit\"></p></form>\
</div>\
</body></html>"
.as_ref() .as_ref()
) )
); );

View File

@ -3,10 +3,7 @@
!}} !}}
{{> ../deep/welcome id = "welcome", tag = "h1", tail = '!' ~}} {{> ../deep/welcome id = "welcome", tag = "h1", tail = '!' ~}}
<div> <div>
{{! Title !}}
<h3>What is your name?</h3> <h3>What is your name?</h3>
{{! Form !}}
<form> <form>
{{! Input name !}} {{! Input name !}}
Name: <input type="text" name="name" /><br/> Name: <input type="text" name="name" /><br/>

View File

@ -5,4 +5,7 @@
- id - id
- tail - tail
!}} !}}
{{#unless tag.is_match(r"^p|(h[1-6])$") && !id.is_empty() }}
{{$ "Need static args: tag: str /^h[1-6]$/, id: str" }}
{{/unless }}
<{{ tag }} id="{{ id }}" class="welcome">Welcome{{ tail }}</{{ tag }}> <{{ tag }} id="{{ id }}" class="welcome">Welcome{{ tail }}</{{ tag }}>

View File

@ -1,3 +1,6 @@
{{# unless title.is_str() && !title.is_empty() }}
{{$ "Need static args: title: str" }}
{{/unless}}
<head> <head>
<meta charset="utf-8"/> <meta charset="utf-8"/>
<title>{{ title }}</title> <title>{{ title }}</title>