diff options
-rw-r--r-- | src/controllers/home_controller.rs | 14 | ||||
-rw-r--r-- | src/controllers/posts_controller.rs | 30 | ||||
-rw-r--r-- | src/utils/mod.rs | 10 | ||||
-rw-r--r-- | templates/edit_post.html | 46 | ||||
-rw-r--r-- | templates/index.html | 12 | ||||
-rw-r--r-- | templates/layout.html | 9 | ||||
-rw-r--r-- | templates/new_post.html | 44 | ||||
-rw-r--r-- | templates/show_post.html | 30 |
8 files changed, 98 insertions, 97 deletions
diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index c4c0c56..563be40 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -3,16 +3,20 @@ use rocket_contrib::Json; #[derive(BartDisplay, Serialize)] #[template = "templates/index.html"] -struct IndexTemplate<'a> { - title: &'a str, +struct IndexTemplate { posts: Vec<models::Post> } -implement_responder_for!(IndexTemplate<'a>); +implement_responder_for!(IndexTemplate); #[get("/", format = "text/html")] -fn index<'a>(conn: utils::DbConn) -> IndexTemplate<'a> { - IndexTemplate { title: "Bloggen", posts: models::Post::get_all(conn) } +fn index(conn: utils::DbConn) -> utils::Page<IndexTemplate> { + utils::Page { + title: String::from("Bloggen"), + content: IndexTemplate { + posts: models::Post::get_all(conn) + } + } } #[get("/", format = "application/json")] diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs index a8c5e1c..38618d3 100644 --- a/src/controllers/posts_controller.rs +++ b/src/controllers/posts_controller.rs @@ -4,16 +4,20 @@ use utils; #[derive(BartDisplay)] #[template = "templates/new_post.html"] -pub struct NewPostTemplate<'a> { - title: &'a str, +pub struct NewPostTemplate { post: ::models::NewPost } -implement_responder_for!(NewPostTemplate<'a>); +implement_responder_for!(NewPostTemplate); #[get("/new", format = "text/html")] -fn new<'a>(_conn: utils::DbConn) -> NewPostTemplate<'a> { - NewPostTemplate { title: "Bloggen", post: Default::default() } +fn new(_conn: utils::DbConn) -> utils::Page<NewPostTemplate> { + utils::Page { + title: String::from("New post"), + content: NewPostTemplate { + post: Default::default() + } + } } #[post("/create", data="<post>")] @@ -31,9 +35,14 @@ pub struct ShowPostTemplate { implement_responder_for!(ShowPostTemplate); #[get("/<id>", format = "text/html")] -fn show<'a>(id: i32, conn: utils::DbConn) -> ShowPostTemplate { +fn show(id: i32, conn: utils::DbConn) -> utils::Page<ShowPostTemplate> { let p = ::models::Post::get(id, conn); - ShowPostTemplate { post: p } + utils::Page { + title: p.title.clone(), + content: ShowPostTemplate { + post: p + }, + } } #[derive(BartDisplay)] @@ -45,9 +54,12 @@ pub struct EditPostTemplate { implement_responder_for!(EditPostTemplate); #[get("/<id>/edit", format = "text/html")] -fn edit(id: i32, conn: utils::DbConn) -> EditPostTemplate { +fn edit(id: i32, conn: utils::DbConn) -> utils::Page<EditPostTemplate> { let p = ::models::Post::get(id, conn); - EditPostTemplate { post: p } + utils::Page { + title: String::from("Edit post"), + content: EditPostTemplate { post: p } + } } #[post("/update", data="<post>")] diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a1ece8b..2e79934 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,4 +1,5 @@ use diesel::pg::PgConnection; +use std::fmt::Display; use r2d2; use r2d2_diesel::ConnectionManager; @@ -44,6 +45,13 @@ impl Deref for DbConn { } } +#[derive(BartDisplay, Serialize)] +#[template = "templates/layout.html"] +pub struct Page<T: Display> { + pub title: String, + pub content: T +} + macro_rules! implement_responder_for { // Implement a responder for the given template type // @@ -52,7 +60,7 @@ macro_rules! implement_responder_for { // So it will have to be passed in to the argument at // the macro incovation instead. ($template_type:ty) => ( - impl<'a> ::rocket::response::Responder<'a> for $template_type { + impl<'a> ::rocket::response::Responder<'a> for ::utils::Page<$template_type> { fn respond_to(self, _: &::rocket::Request) -> Result<::rocket::Response<'static>, ::rocket::http::Status> { ::rocket::Response::build() .header(::rocket::http::ContentType::HTML) diff --git a/templates/edit_post.html b/templates/edit_post.html index 9154e3d..ef9c79a 100644 --- a/templates/edit_post.html +++ b/templates/edit_post.html @@ -1,30 +1,22 @@ -<!DOCTYPE html> -<html> - <head> - <title>Edit {{ post.title }}</title> - </head> - <body> - <h1>Edit post</h1> - <form id="new_post" name="post" method="post" action="/posts/update"> - <input type="hidden" name="id" value="{{ post.id }}"> - <div class="field"> - <label>Title:</label> - <input type="text" name="title" value="{{ post.title }}"> - </div> +<h1>Edit post</h1> +<form id="new_post" name="post" method="post" action="/posts/update"> + <input type="hidden" name="id" value="{{ post.id }}"> + <div class="field"> + <label>Title:</label> + <input type="text" name="title" value="{{ post.title }}"> + </div> - <div class="field"> - <label>Contents:</label> - <textarea name="body">{{ post.body }}</textarea> - </div> + <div class="field"> + <label>Contents:</label> + <textarea name="body">{{ post.body }}</textarea> + </div> - <div class="field"> - <input type="checkbox" checked="{{ post.published }}"> - <label>Published</label> - </div> + <div class="field"> + <input type="checkbox" checked="{{ post.published }}"> + <label>Published</label> + </div> - <div class="actions"> - <input type="submit" value="Save"> - </div> - </form> - </body> -</html> + <div class="actions"> + <input type="submit" value="Save"> + </div> +</form> diff --git a/templates/index.html b/templates/index.html index 674b867..1b40864 100644 --- a/templates/index.html +++ b/templates/index.html @@ -1,10 +1,2 @@ -<!DOCTYPE html> -<html> - <head> - <title>{{ title }}</title> - </head> - <body> - <p><a href="/posts/new">Create new post</a></p> - {{# posts }}{{> post_teaser.html }}{{/ posts }} - </body> -</html> +<p><a href="/posts/new">Create new post</a></p> +{{# posts }}{{> post_teaser.html }}{{/ posts }} diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..09ecd1c --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,9 @@ +<!DOCTYPE html> +<html> + <head> + <title>{{ title }}</title> + </head> + <body> + {{{ content }}} + </body> +</html> diff --git a/templates/new_post.html b/templates/new_post.html index 93de696..a3a1a0f 100644 --- a/templates/new_post.html +++ b/templates/new_post.html @@ -1,29 +1,21 @@ -<!DOCTYPE html> -<html> - <head> - <title>{{ title }}</title> - </head> - <body> - <h1>Create a new post</h1> - <form id="new_post" name="post" method="post" action="/posts/create"> - <div class="field"> - <label>Title:</label> - <input type="text" name="title" value="{{ post.title }}"> - </div> +<h1>Create a new post</h1> +<form id="new_post" name="post" method="post" action="/posts/create"> + <div class="field"> + <label>Title:</label> + <input type="text" name="title" value="{{ post.title }}"> + </div> - <div class="field"> - <label>Contents:</label> - <textarea name="body">{{ post.body }}</textarea> - </div> + <div class="field"> + <label>Contents:</label> + <textarea name="body">{{ post.body }}</textarea> + </div> - <div class="field"> - <input type="checkbox" checked="{{ post.published }}"> - <label>Published</label> - </div> + <div class="field"> + <input type="checkbox" checked="{{ post.published }}"> + <label>Published</label> + </div> - <div class="actions"> - <input type="submit" value="Create post"> - </div> - </form> - </body> -</html> + <div class="actions"> + <input type="submit" value="Create post"> + </div> +</form> diff --git a/templates/show_post.html b/templates/show_post.html index 31c2ca3..9e633cc 100644 --- a/templates/show_post.html +++ b/templates/show_post.html @@ -1,19 +1,11 @@ -<!DOCTYPE html> -<html> - <head> - <title>{{ post.title }}</title> - </head> - <body> - <article class="post-teaser"> - {{# post. }} - <header> - <h1>{{ .title }}</h1> - {{> post_actions.html }} - </header> - <section class="teaser"> - {{ .body }} - </section> - {{/ post }} - </article> - </body> -</html> +<article class="post-teaser"> + {{# post. }} + <header> + <h1>{{ .title }}</h1> + {{> post_actions.html }} + </header> + <section class="teaser"> + {{ .body }} + </section> + {{/ post }} +</article> |