From 006b7f3d08334e5792d22671c7d11c405c67998f Mon Sep 17 00:00:00 2001 From: Alexandru Tiniuc Date: Sat, 8 Jun 2019 18:47:39 +0100 Subject: [PATCH] adds Handlebars example Closes #122 --- Cargo.toml | 1 + template_handlebars/Cargo.toml | 10 ++++ template_handlebars/src/README.md | 7 +++ template_handlebars/src/main.rs | 59 +++++++++++++++++++ .../static/templates/index.html | 12 ++++ .../static/templates/user.html | 12 ++++ 6 files changed, 101 insertions(+) create mode 100644 template_handlebars/Cargo.toml create mode 100644 template_handlebars/src/README.md create mode 100644 template_handlebars/src/main.rs create mode 100644 template_handlebars/static/templates/index.html create mode 100644 template_handlebars/static/templates/user.html diff --git a/Cargo.toml b/Cargo.toml index 14ad4224..a35948e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ members = [ "template_askama", "template_tera", "template_yarte", + "template_handlebars", "tls", #"unix-socket", "web-cors/backend", diff --git a/template_handlebars/Cargo.toml b/template_handlebars/Cargo.toml new file mode 100644 index 00000000..11fb0c58 --- /dev/null +++ b/template_handlebars/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "template_handlebars" +version = "0.1.0" +authors = ["Alexandru Tiniuc "] +edition = "2018" + +[dependencies] +actix-web = "1.0" +handlebars = "2.0.0-beta.2" +serde_json = "1.0" \ No newline at end of file diff --git a/template_handlebars/src/README.md b/template_handlebars/src/README.md new file mode 100644 index 00000000..88139535 --- /dev/null +++ b/template_handlebars/src/README.md @@ -0,0 +1,7 @@ +# Handlebars + +This is an example of how to use Actix Web with the [Handlebars templating language](https://crates.io/crates/handlebars), which is currently the most popular crate that achieves this. After starting the server with `cargo run`, you may visit the following pages: + +- http://localhost:8080 +- http://localhost:8080/Emma/documents +- http://localhost:8080/Bob/passwordds \ No newline at end of file diff --git a/template_handlebars/src/main.rs b/template_handlebars/src/main.rs new file mode 100644 index 00000000..e49c7ad0 --- /dev/null +++ b/template_handlebars/src/main.rs @@ -0,0 +1,59 @@ +#[macro_use] +extern crate actix_web; + +#[macro_use] +extern crate serde_json; + +use actix_web::web; +use actix_web::{App, HttpResponse, HttpServer}; + +use handlebars::Handlebars; + +use std::sync::Arc; + +use std::io; + +// Macro documentation can be found in the actix_web_codegen crate +#[get("/")] +fn index(hb: web::Data>) -> HttpResponse { + let data = json!({ + "name": "Handlebars" + }); + let body = hb.render("index", &data).unwrap(); + + HttpResponse::Ok().body(body) +} + +#[get("/{user}/{data}")] +fn user( + hb: web::Data>, + info: web::Path<(String, String)>, +) -> HttpResponse { + let data = json!({ + "user": info.0, + "data": info.1 + }); + let body = hb.render("user", &data).unwrap(); + + HttpResponse::Ok().body(body) +} + +fn main() -> io::Result<()> { + // Handlebars uses a repository for the compiled templates. This object must be + // shared between the application threads, and is therefore passed to the + // Application Builder as an atomic reference-counted pointer. + let mut handlebars = Handlebars::new(); + handlebars + .register_templates_directory(".html", "./static/templates") + .unwrap(); + let handlebars_ref = web::Data::new(Arc::new(handlebars)); + + HttpServer::new(move || { + App::new() + .register_data(handlebars_ref.clone()) + .service(index) + .service(user) + }) + .bind("127.0.0.1:8080")? + .run() +} diff --git a/template_handlebars/static/templates/index.html b/template_handlebars/static/templates/index.html new file mode 100644 index 00000000..867c114e --- /dev/null +++ b/template_handlebars/static/templates/index.html @@ -0,0 +1,12 @@ + + + + + {{name}} Example + + + +

{{name}} example

+

This is an example of how to use {{name}} with Actix-Web.

+ + \ No newline at end of file diff --git a/template_handlebars/static/templates/user.html b/template_handlebars/static/templates/user.html new file mode 100644 index 00000000..59e2f1fa --- /dev/null +++ b/template_handlebars/static/templates/user.html @@ -0,0 +1,12 @@ + + + + + {{user}}'s homepage + + + +

Welcome back, {{user}}

+

Here's your {{data}}.

+ + \ No newline at end of file