From 114b777b806f793ebfe21895830bae99f524c9d5 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 16 Nov 2017 17:15:06 +0100 Subject: Pass posts to the template and iterate over them. --- src/main.rs | 8 ++------ templates/index.html | 6 ++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8dd6771..55131fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ use self::rocket_blog::{schema, models}; #[template = "templates/index.html"] struct IndexTemplate<'a> { title: &'a str, - content: &'a str + posts: Vec } #[get("/")] @@ -24,11 +24,7 @@ fn index(conn: rocket_blog::DbConn) -> String { .load::(&*conn) .expect("Error loading posts"); - let output : String = results.iter().map(|post| { - format!("{}", &IndexTemplate{ title: &post.title, content: &post.body }) - }).collect(); - - output + format!("{}", &IndexTemplate{ title: "Bloggen", posts: results }) } fn main() { diff --git a/templates/index.html b/templates/index.html index c2a7b92..a4279bd 100644 --- a/templates/index.html +++ b/templates/index.html @@ -4,6 +4,8 @@ {{ title }} - {{ content }} +
    + {{# posts }}
  • {{ .title }}
  • {{/ posts }} +
- \ No newline at end of file + -- cgit v1.2.3