From 3e8a6c39881c5867fd72fb2eb9619e2292fe6968 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Mon, 18 Dec 2017 13:41:52 -0800 Subject: [PATCH] add tera example --- examples/basic.rs | 2 +- examples/template_tera/Cargo.toml | 10 +++++ examples/template_tera/src/main.rs | 44 +++++++++++++++++++++ examples/template_tera/templates/index.html | 17 ++++++++ examples/template_tera/templates/user.html | 13 ++++++ 5 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 examples/template_tera/Cargo.toml create mode 100644 examples/template_tera/src/main.rs create mode 100644 examples/template_tera/templates/index.html create mode 100644 examples/template_tera/templates/user.html diff --git a/examples/basic.rs b/examples/basic.rs index 9c182817c..d6b8b3a9e 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -56,7 +56,7 @@ fn with_param(req: HttpRequest) -> Result fn main() { ::std::env::set_var("RUST_LOG", "actix_web=info"); let _ = env_logger::init(); - let sys = actix::System::new("ws-example"); + let sys = actix::System::new("basic-example"); HttpServer::new( || Application::new() diff --git a/examples/template_tera/Cargo.toml b/examples/template_tera/Cargo.toml new file mode 100644 index 000000000..b5ce86cad --- /dev/null +++ b/examples/template_tera/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "template-tera" +version = "0.1.0" +authors = ["Nikolay Kim "] + +[dependencies] +env_logger = "0.4" +actix = "^0.3.1" +actix-web = { git = "https://github.com/actix/actix-web.git" } +tera = "*" diff --git a/examples/template_tera/src/main.rs b/examples/template_tera/src/main.rs new file mode 100644 index 000000000..97b9d4812 --- /dev/null +++ b/examples/template_tera/src/main.rs @@ -0,0 +1,44 @@ +extern crate actix; +extern crate actix_web; +extern crate env_logger; +#[macro_use] +extern crate tera; +use actix_web::*; + +struct State { + template: tera::Tera, // <- store tera template in application state +} + +fn index(req: HttpRequest) -> HttpResponse { + let s = if let Some(name) = req.query().get("name") { // <- submitted form + let mut ctx = tera::Context::new(); + ctx.add("name", name); + ctx.add("text", &"Welcome!".to_owned()); + req.state().template.render("user.html", &ctx).unwrap() + } else { + req.state().template.render("index.html", &tera::Context::new()).unwrap() + }; + httpcodes::HTTPOk.build() + .content_type("text/html") + .body(s) + .unwrap() +} + +fn main() { + ::std::env::set_var("RUST_LOG", "actix_web=info"); + let _ = env_logger::init(); + let sys = actix::System::new("tera-example"); + + HttpServer::new(|| { + let tera = compile_templates!(concat!(env!("CARGO_MANIFEST_DIR"), "/templates/**/*")); + + Application::with_state(State{template: tera}) + // enable logger + .middleware(middlewares::Logger::default()) + .resource("/", |r| r.method(Method::GET).f(index))}) + .bind("127.0.0.1:8080").unwrap() + .start().unwrap(); + + println!("Started http server: 127.0.0.1:8080"); + let _ = sys.run(); +} diff --git a/examples/template_tera/templates/index.html b/examples/template_tera/templates/index.html new file mode 100644 index 000000000..d8a47bc09 --- /dev/null +++ b/examples/template_tera/templates/index.html @@ -0,0 +1,17 @@ + + + + + Actix web + + +

Welcome!

+

+

What is your name?

+
+
+

+
+

+ + diff --git a/examples/template_tera/templates/user.html b/examples/template_tera/templates/user.html new file mode 100644 index 000000000..cb5328915 --- /dev/null +++ b/examples/template_tera/templates/user.html @@ -0,0 +1,13 @@ + + + + + Actix web + + +

Hi, {{ name }}!

+

+ {{ text }} +

+ +