From 3bc7143ced57da953969d80d99ff8466ab986ae2 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 16 Nov 2017 16:40:38 +0100 Subject: Add Bart templating engine. --- Cargo.toml | 2 ++ src/main.rs | 25 +++++++++++++++---------- templates/index.html | 9 +++++++++ 3 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 templates/index.html diff --git a/Cargo.toml b/Cargo.toml index 78fbf3a..1e69352 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ diesel_codegen = { version = "0.16.0", features = ["postgres"] } dotenv = "0.9.0" r2d2 = "0.7.4" r2d2-diesel = "0.16.0" +bart = "0.1.4" +bart_derive = "0.1.4" diff --git a/src/main.rs b/src/main.rs index 622ac1c..8dd6771 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,27 +1,32 @@ #![feature(plugin)] #![plugin(rocket_codegen)] +extern crate diesel; extern crate rocket; extern crate rocket_blog; +#[macro_use] extern crate bart_derive; -extern crate diesel; use self::diesel::prelude::*; +use self::rocket_blog::{schema, models}; + +#[derive(BartDisplay)] +#[template = "templates/index.html"] +struct IndexTemplate<'a> { + title: &'a str, + content: &'a str +} #[get("/")] fn index(conn: rocket_blog::DbConn) -> String { - use rocket_blog::schema::posts::dsl::*; + use schema::posts::dsl::*; let results = posts.filter(published.eq(false)) .limit(5) - .load::(&*conn) + .load::(&*conn) .expect("Error loading posts"); - let mut output = String::from("Hallo, for faen!\n\n"); - - for post in results { - output += &format!("{}\n", post.title); - output += "================\n"; - output += &format!("{}\n", post.body); - } + let output : String = results.iter().map(|post| { + format!("{}", &IndexTemplate{ title: &post.title, content: &post.body }) + }).collect(); output } diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..c2a7b92 --- /dev/null +++ b/templates/index.html @@ -0,0 +1,9 @@ + + + + {{ title }} + + + {{ content }} + + \ No newline at end of file -- cgit v1.2.3