mirror of
https://github.com/actix/examples
synced 2024-12-02 18:02:22 +01:00
Update yarte to 0.2
This commit is contained in:
parent
273068c362
commit
8646a89249
@ -1,15 +1,23 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "example"
|
name = "template_yarte"
|
||||||
version = "0.0.1"
|
version = "0.0.1"
|
||||||
authors = ["Rust-iendo Barcelona <riendocontributions@gmail.com>"]
|
authors = ["Rust-iendo Barcelona <riendocontributions@gmail.com>"]
|
||||||
publish = false
|
publish = false
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
workspace = ".."
|
workspace = ".."
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "1.0.0-alpha.2"
|
|
||||||
env_logger = "0.6"
|
env_logger = "0.6"
|
||||||
yarte = "0.1"
|
|
||||||
|
yarte = { version = "0.2", features=["with-actix-web"] }
|
||||||
|
|
||||||
|
actix-web = "1.0.0-alpha"
|
||||||
|
|
||||||
[build-dependencies]
|
[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"
|
||||||
|
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
Example of composition with partials and `with-actix-web` feature
|
Example of composition with partials and `with-actix-web` feature
|
||||||
|
|
||||||
See the generated code on stdout when run at debug
|
|
||||||
```bash
|
```bash
|
||||||
|
cargo test
|
||||||
|
|
||||||
cargo run
|
cargo run
|
||||||
```
|
```
|
||||||
> open `localhost:8080`
|
> open `localhost:8080`
|
||||||
|
|
||||||
More at [mdbook](https://yarte.netlify.com/) and [repository](https://gitlab.com/r-iendo/yarte)
|
|
||||||
|
@ -1 +0,0 @@
|
|||||||
|
|
@ -3,28 +3,18 @@ extern crate actix_web;
|
|||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
use actix_web::{
|
use actix_web::{middleware::Logger, web, App, HttpServer, Responder};
|
||||||
error::ErrorInternalServerError, middleware, web::Query, App, HttpResponse,
|
|
||||||
HttpServer, Result,
|
|
||||||
};
|
|
||||||
use yarte::Template;
|
use yarte::Template;
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
#[template(path = "index.hbs")]
|
#[template(path = "index.hbs")]
|
||||||
struct IndexTemplate {
|
struct IndexTemplate {
|
||||||
query: Query<HashMap<String, String>>,
|
query: web::Query<HashMap<String, String>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/")]
|
#[get("/")]
|
||||||
pub fn index(query: Query<HashMap<String, String>>) -> Result<HttpResponse> {
|
pub fn index(query: web::Query<HashMap<String, String>>) -> impl Responder {
|
||||||
IndexTemplate { query }
|
IndexTemplate { query }
|
||||||
.call()
|
|
||||||
.map(|s| {
|
|
||||||
HttpResponse::Ok()
|
|
||||||
.content_type(IndexTemplate::mime())
|
|
||||||
.body(s)
|
|
||||||
})
|
|
||||||
.map_err(|_| ErrorInternalServerError("Template parsing error"))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> std::io::Result<()> {
|
fn main() -> std::io::Result<()> {
|
||||||
@ -32,12 +22,104 @@ fn main() -> std::io::Result<()> {
|
|||||||
env_logger::init();
|
env_logger::init();
|
||||||
|
|
||||||
// start http server
|
// start http server
|
||||||
HttpServer::new(|| {
|
HttpServer::new(move || App::new().wrap(Logger::default()).service(index))
|
||||||
App::new()
|
|
||||||
// enable logger
|
|
||||||
.wrap(middleware::Logger::default())
|
|
||||||
.service(index)
|
|
||||||
})
|
|
||||||
.bind("127.0.0.1:8080")?
|
.bind("127.0.0.1:8080")?
|
||||||
.run()
|
.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()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
20
template_yarte/templates/deep/more/card/form.hbs
Normal file
20
template_yarte/templates/deep/more/card/form.hbs
Normal 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>
|
8
template_yarte/templates/deep/more/card/hi.hbs
Normal file
8
template_yarte/templates/deep/more/card/hi.hbs
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
{{!
|
||||||
|
Hi message:
|
||||||
|
args:
|
||||||
|
- lastname
|
||||||
|
- name
|
||||||
|
!}}
|
||||||
|
<h1>Hi, {{ name }} {{ lastname }}!</h1>
|
||||||
|
{{~> alias/welcome id = "hi", tag = 'p', tail = "" }}
|
@ -1 +1,8 @@
|
|||||||
<{{ tag }}>Welcome!</{{ tag }}>
|
{{!
|
||||||
|
Welcome card:
|
||||||
|
args:
|
||||||
|
- tag
|
||||||
|
- id
|
||||||
|
- tail
|
||||||
|
!}}
|
||||||
|
<{{ tag }} id="{{ id }}" class="welcome">Welcome{{ tail }}</{{ tag }}>
|
||||||
|
1
template_yarte/templates/deep/more/doc/head.hbs
Normal file
1
template_yarte/templates/deep/more/doc/head.hbs
Normal file
@ -0,0 +1 @@
|
|||||||
|
<head><meta charset="utf-8" /><title>{{ title }}</title></head>
|
1
template_yarte/templates/deep/more/doc/t.hbs
Normal file
1
template_yarte/templates/deep/more/doc/t.hbs
Normal file
@ -0,0 +1 @@
|
|||||||
|
<!DOCTYPE html>
|
@ -1,22 +1,12 @@
|
|||||||
<!DOCTYPE html>
|
{{! Simple example !}}
|
||||||
|
{{> doc/t ~}}
|
||||||
<html>
|
<html>
|
||||||
<head>
|
{{~> doc/head title = "Actix web" ~}}
|
||||||
<meta charset="utf-8"/>
|
|
||||||
<title>Actix web</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
<body>
|
||||||
{{~#if let Some(name) = query.get("name") ~}}
|
{{~#if let Some(name) = query.get("name") }}
|
||||||
<h1>Hi, {{ name }}!</h1>
|
{{ let lastname = query.get("lastname").ok_or(yarte::Error)? }}
|
||||||
{{~> alias/welcome tag='p' ~}}
|
{{> card/hi ~}}
|
||||||
{{~ else ~}}
|
{{ else ~}}
|
||||||
{{~> alias/welcome tag="h1" ~}}
|
{{> card/form ~}}
|
||||||
<p>
|
{{/if ~}}
|
||||||
<h3>What is your name?</h3>
|
</body></html>
|
||||||
<form>
|
|
||||||
<input type="text" name="name"/><br/>
|
|
||||||
<p><input type="submit"></p>
|
|
||||||
</form>
|
|
||||||
</p>
|
|
||||||
{{~/if~}}
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
@ -5,3 +5,5 @@ dir = "templates"
|
|||||||
# Alias for partials. In call, change the start of partial path with one of this, if exist.
|
# Alias for partials. In call, change the start of partial path with one of this, if exist.
|
||||||
[partials]
|
[partials]
|
||||||
alias = "./deep/more/deep"
|
alias = "./deep/more/deep"
|
||||||
|
doc = "./deep/more/doc"
|
||||||
|
card = "./deep/more/card"
|
||||||
|
Loading…
Reference in New Issue
Block a user