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. --- src/main.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) (limited to 'src/main.rs') 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 } -- cgit v1.2.3