diff --git a/.travis.yml b/.travis.yml index 66f91d8b..4c82f956 100644 --- a/.travis.yml +++ b/.travis.yml @@ -60,6 +60,7 @@ script: cd static_index && cargo check && cd .. cd template_askama && cargo check && cd .. cd template_tera && cargo check && cd .. + cd template_yarte && cargo check && cd .. cd tls && cargo check && cd .. cd rustls && cargo check && cd .. cd unix-socket && cargo check && cd .. diff --git a/Cargo.toml b/Cargo.toml index 5c287311..7422c22d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ members = [ "static_index", "template_askama", "template_tera", + "template_yarte", "tls", "rustls", "unix-socket", diff --git a/template_yarte/Cargo.toml b/template_yarte/Cargo.toml new file mode 100644 index 00000000..4a7f8cbb --- /dev/null +++ b/template_yarte/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "example" +version = "0.0.1" +authors = ["Rust-iendo Barcelona "] +publish = false +edition = "2018" + +workspace = ".." + +[dependencies] +env_logger = "0.6" + +yarte = "0.1" +actix-web = { git="https://github.com/actix/actix-web.git", branch = "1.0" } + +[build-dependencies] +yarte = "0.1" diff --git a/template_yarte/README.md b/template_yarte/README.md new file mode 100644 index 00000000..df534a7c --- /dev/null +++ b/template_yarte/README.md @@ -0,0 +1,11 @@ +# yarte + +Example of composition with partials and `with-actix-web` feature + +See the generated code on stdout when run at debug +```bash +cargo run +``` +> open `localhost:8080` + +More at [mdbook](https://yarte.netlify.com/) and [repository](https://gitlab.com/r-iendo/yarte) diff --git a/template_yarte/build.rs b/template_yarte/build.rs new file mode 100644 index 00000000..da256d18 --- /dev/null +++ b/template_yarte/build.rs @@ -0,0 +1,5 @@ +use yarte::recompile; + +fn main() { + recompile::when_changed(); +} diff --git a/template_yarte/src/lib.rs b/template_yarte/src/lib.rs new file mode 100644 index 00000000..83f1f848 --- /dev/null +++ b/template_yarte/src/lib.rs @@ -0,0 +1,21 @@ +use actix_web::{error::ErrorInternalServerError, web::Query, HttpResponse, Result}; +use yarte::Template; + +use std::collections::HashMap; + +#[derive(Template)] +#[template(path = "index.hbs")] +struct IndexTemplate { + query: Query>, +} + +pub fn index(query: Query>) -> Result { + IndexTemplate { query } + .call() + .map(|s| { + HttpResponse::Ok() + .content_type(IndexTemplate::mime()) + .body(s) + }) + .map_err(|_| ErrorInternalServerError("Template parsing error")) +} diff --git a/template_yarte/src/main.rs b/template_yarte/src/main.rs new file mode 100644 index 00000000..6a4a896e --- /dev/null +++ b/template_yarte/src/main.rs @@ -0,0 +1,19 @@ +use actix_web::{middleware, web, App, HttpServer}; + +#[path = "lib.rs"] +mod template; + +fn main() -> std::io::Result<()> { + std::env::set_var("RUST_LOG", "actix_web=info"); + env_logger::init(); + + // start http server + HttpServer::new(|| { + App::new() + // enable logger + .middleware(middleware::Logger::default()) + .service(web::resource("/").to(template::index)) + }) + .bind("127.0.0.1:8080")? + .run() +} diff --git a/template_yarte/templates/deep/more/deep/welcome.hbs b/template_yarte/templates/deep/more/deep/welcome.hbs new file mode 100644 index 00000000..5cb2a971 --- /dev/null +++ b/template_yarte/templates/deep/more/deep/welcome.hbs @@ -0,0 +1 @@ +<{{ tag }}>Welcome! diff --git a/template_yarte/templates/index.hbs b/template_yarte/templates/index.hbs new file mode 100644 index 00000000..503bb845 --- /dev/null +++ b/template_yarte/templates/index.hbs @@ -0,0 +1,22 @@ + + + + + Actix web + + +{{~#if let Some(name) = query.get("name") ~}} +

Hi, {{ name }}!

+ {{~> alias/welcome tag='p' ~}} +{{~ else ~}} + {{~> alias/welcome tag="h1" ~}} +

+

What is your name?

+
+
+

+
+

+{{~/if~}} + + diff --git a/template_yarte/yarte.toml b/template_yarte/yarte.toml new file mode 100644 index 00000000..19a63673 --- /dev/null +++ b/template_yarte/yarte.toml @@ -0,0 +1,14 @@ +# root dir of templates +[main] +dir = "templates" +debug = "code" + +# Alias for partials. In call, change the start of partial path with one of this, if exist. +[partials] +alias = "./deep/more/deep" + +[debug] +# prettyprint themes, put anything for options +theme = "zenburn" +number_line = true +grid = true