From 0096ed535d58552a3882e6b6672d5944abf40b81 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sat, 20 Jan 2018 14:02:36 +0100 Subject: Refactor index template. Use a vector of show post templates instead of manually decoding posts in the index view. --- src/controllers/home_controller.rs | 7 +++++-- src/controllers/posts_controller.rs | 4 ++-- templates/index.html | 2 +- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index d9a539a..2f657c3 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -2,11 +2,12 @@ use ::{models, utils}; use rocket; use rocket_contrib::Json; use std::path::PathBuf; +use posts_controller::ShowPostTemplate; #[derive(BartDisplay, Serialize)] #[template = "templates/index.html"] struct IndexTemplate { - posts: Vec + posts: Vec } implement_responder_for!(IndexTemplate); @@ -17,7 +18,9 @@ fn index(flash: Option, conn: utils::DbConn) -> u title: String::from("Bloggen"), flash: flash.map_or(None, |f| Some(f.msg().to_string())), content: IndexTemplate { - posts: models::Post::get_all(conn) + posts: models::Post::get_all(conn).into_iter() + .map(|p| ShowPostTemplate { post: p }) + .collect() } } } diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index 5fdd60e..47f9ca1 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -27,10 +27,10 @@ fn create(post: Form<::models::NewPost>, conn: utils::DbConn) -> Flash Flash::success(Redirect::to("/"), "Post successfully created!") } -#[derive(BartDisplay)] +#[derive(BartDisplay, Serialize)] #[template = "templates/show_post.html"] pub struct ShowPostTemplate { - post: ::models::Post + pub post: ::models::Post } implement_responder_for!(ShowPostTemplate); diff --git a/templates/index.html b/templates/index.html index 1b40864..151af39 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,2 +1,2 @@

Create new post

-{{# posts }}{{> post_teaser.html }}{{/ posts }} +{{# posts }}{{{ . }}}{{/ posts }} -- cgit v1.2.3