From 4a37a91ece830e80f6f8287cacf95c1e3b993e89 Mon Sep 17 00:00:00 2001 From: Magnus Hoff Date: Sun, 20 Aug 2017 22:34:26 +0200 Subject: [PATCH] Split layout out from rendering responses --- src/site.rs | 14 +++++++++++++- templates/article.html | 8 -------- templates/layout.html | 9 +++++++++ 3 files changed, 22 insertions(+), 9 deletions(-) create mode 100644 templates/layout.html diff --git a/src/site.rs b/src/site.rs index d832799..9d4ec70 100644 --- a/src/site.rs +++ b/src/site.rs @@ -1,3 +1,5 @@ +use std::fmt; + use futures::{self, Future}; use hyper; use hyper::header::ContentType; @@ -9,6 +11,13 @@ lazy_static! { static ref TEXT_HTML: mime::Mime = "text/html;charset=utf-8".parse().unwrap(); } +#[derive(BartDisplay)] +#[template = "templates/layout.html"] +pub struct Layout<'a, T: 'a + fmt::Display> { + pub title: &'a str, + pub body: &'a T, +} + pub struct Site { state: State } @@ -44,7 +53,10 @@ impl Service for Site { futures::finished( Response::new() .with_header(ContentType(TEXT_HTML.clone())) - .with_body(format!("{}", article)) + .with_body(Layout { + title: &article.title, + body: &article + }.to_string()) .with_status(hyper::StatusCode::Ok) ).boxed() }, diff --git a/templates/article.html b/templates/article.html index c675879..f2d8477 100644 --- a/templates/article.html +++ b/templates/article.html @@ -1,13 +1,5 @@ - - - -{{title}} - -

{{id}}-{{revision}}

{{created}}

{{title}}

{{body}}

- - diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..9000d21 --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,9 @@ + + + +{{title}} + + +{{{body}}} + +