1
0
mirror of https://github.com/actix/examples synced 2024-11-27 16:02:57 +01:00

Update yarte to 0.2

This commit is contained in:
Juan Aguilar Santillana 2019-04-01 01:23:31 +02:00
parent 273068c362
commit 8646a89249
11 changed files with 167 additions and 49 deletions

View File

@ -1,15 +1,23 @@
[package]
name = "example"
name = "template_yarte"
version = "0.0.1"
authors = ["Rust-iendo Barcelona <riendocontributions@gmail.com>"]
publish = false
edition = "2018"
workspace = ".."
[dependencies]
actix-web = "1.0.0-alpha.2"
env_logger = "0.6"
yarte = "0.1"
yarte = { version = "0.2", features=["with-actix-web"] }
actix-web = "1.0.0-alpha"
[build-dependencies]
yarte = "0.1"
yarte = { version = "0.2", features=["with-actix-web"] }
[dev-dependencies]
bytes = "0.4"
actix-http-test = "0.1.0-alpha"
actix-http = "0.1.0-alpha"

View File

@ -2,10 +2,10 @@
Example of composition with partials and `with-actix-web` feature
See the generated code on stdout when run at debug
```bash
cargo test
cargo run
```
> open `localhost:8080`
More at [mdbook](https://yarte.netlify.com/) and [repository](https://gitlab.com/r-iendo/yarte)

View File

@ -1 +0,0 @@

View File

@ -3,28 +3,18 @@ extern crate actix_web;
use std::collections::HashMap;
use actix_web::{
error::ErrorInternalServerError, middleware, web::Query, App, HttpResponse,
HttpServer, Result,
};
use actix_web::{middleware::Logger, web, App, HttpServer, Responder};
use yarte::Template;
#[derive(Template)]
#[template(path = "index.hbs")]
struct IndexTemplate {
query: Query<HashMap<String, String>>,
query: web::Query<HashMap<String, String>>,
}
#[get("/")]
pub fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
pub fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
IndexTemplate { query }
.call()
.map(|s| {
HttpResponse::Ok()
.content_type(IndexTemplate::mime())
.body(s)
})
.map_err(|_| ErrorInternalServerError("Template parsing error"))
}
fn main() -> std::io::Result<()> {
@ -32,12 +22,104 @@ fn main() -> std::io::Result<()> {
env_logger::init();
// start http server
HttpServer::new(|| {
App::new()
// enable logger
.wrap(middleware::Logger::default())
.service(index)
})
.bind("127.0.0.1:8080")?
.run()
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
.bind("127.0.0.1:8080")?
.run()
}
#[cfg(test)]
mod test {
use super::*;
use actix_http::HttpService;
use actix_http_test::TestServer;
use actix_web::{http, App};
use bytes::Bytes;
#[test]
fn test() {
let mut srv = TestServer::new(|| HttpService::new(App::new().service(index)));
let req = srv.get();
let response = srv.block_on(req.send()).unwrap();
assert!(response.status().is_success());
assert_eq!(
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html"
);
let bytes = srv.block_on(response.body()).unwrap();
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\" /><title>Actix web</title></head><body>\
<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>\
</body></html>"
.as_ref()
)
);
let req = srv.get().uri(srv.url("/?name=foo&lastname=bar"));
let response = srv.block_on(req.send()).unwrap();
assert!(response.status().is_success());
assert_eq!(
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html"
);
let bytes = srv.block_on(response.body()).unwrap();
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\" /><title>Actix web</title></head>\
<body>\
<h1>Hi, foo bar!</h1><p id=\"hi\" class=\"welcome\">Welcome</p>\
</body></html>"
.as_ref()
)
);
let req = srv.get().uri(srv.url("/?name=foo"));
let response = srv.block_on(req.send()).unwrap();
assert!(response.status().is_server_error());
let req = srv.get().uri(srv.url("/?lastname=bar"));
let response = srv.block_on(req.send()).unwrap();
assert!(response.status().is_success());
assert_eq!(
response.headers().get(http::header::CONTENT_TYPE).unwrap(),
"text/html"
);
let bytes = srv.block_on(response.body()).unwrap();
assert_eq!(
bytes,
Bytes::from_static(
"<!DOCTYPE html>\
<html>\
<head><meta charset=\"utf-8\" /><title>Actix web</title></head><body>\
<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>\
</body></html>"
.as_ref()
)
);
}
}

View File

@ -0,0 +1,20 @@
{{!
Form: What is your name?
!}}
{{> ../deep/welcome id = "welcome", tag = "h1", tail = '!' ~}}
<div>
{{! Title !}}
<h3>What is your name?</h3>
{{! Form !}}
<form>
{{! Input name !}}
Name: <input type="text" name="name" />
{{! Input last name !}}
<br/>Last name: <input type="text" name="lastname" />
{{! Submit !}}
<br/><p><input type="submit"></p></form>
{{! Order and remove whitespace with comments !}}
</div>

View File

@ -0,0 +1,8 @@
{{!
Hi message:
args:
- lastname
- name
!}}
<h1>Hi, {{ name }} {{ lastname }}!</h1>
{{~> alias/welcome id = "hi", tag = 'p', tail = "" }}

View File

@ -1 +1,8 @@
<{{ tag }}>Welcome!</{{ tag }}>
{{!
Welcome card:
args:
- tag
- id
- tail
!}}
<{{ tag }} id="{{ id }}" class="welcome">Welcome{{ tail }}</{{ tag }}>

View File

@ -0,0 +1 @@
<head><meta charset="utf-8" /><title>{{ title }}</title></head>

View File

@ -0,0 +1 @@
<!DOCTYPE html>

View File

@ -1,22 +1,12 @@
<!DOCTYPE html>
{{! Simple example !}}
{{> doc/t ~}}
<html>
<head>
<meta charset="utf-8"/>
<title>Actix web</title>
</head>
{{~> doc/head title = "Actix web" ~}}
<body>
{{~#if let Some(name) = query.get("name") ~}}
<h1>Hi, {{ name }}!</h1>
{{~> alias/welcome tag='p' ~}}
{{~ else ~}}
{{~> alias/welcome tag="h1" ~}}
<p>
<h3>What is your name?</h3>
<form>
<input type="text" name="name"/><br/>
<p><input type="submit"></p>
</form>
</p>
{{~/if~}}
</body>
</html>
{{~#if let Some(name) = query.get("name") }}
{{ let lastname = query.get("lastname").ok_or(yarte::Error)? }}
{{> card/hi ~}}
{{ else ~}}
{{> card/form ~}}
{{/if ~}}
</body></html>

View File

@ -5,3 +5,5 @@ dir = "templates"
# Alias for partials. In call, change the start of partial path with one of this, if exist.
[partials]
alias = "./deep/more/deep"
doc = "./deep/more/doc"
card = "./deep/more/card"