Split layout out from rendering responses

This commit is contained in:
Magnus Hoff 2017-08-20 22:34:26 +02:00
parent 35e7bad7d2
commit 4a37a91ece
3 changed files with 22 additions and 9 deletions

View file

@ -1,3 +1,5 @@
use std::fmt;
use futures::{self, Future}; use futures::{self, Future};
use hyper; use hyper;
use hyper::header::ContentType; use hyper::header::ContentType;
@ -9,6 +11,13 @@ lazy_static! {
static ref TEXT_HTML: mime::Mime = "text/html;charset=utf-8".parse().unwrap(); 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 { pub struct Site {
state: State state: State
} }
@ -44,7 +53,10 @@ impl Service for Site {
futures::finished( futures::finished(
Response::new() Response::new()
.with_header(ContentType(TEXT_HTML.clone())) .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) .with_status(hyper::StatusCode::Ok)
).boxed() ).boxed()
}, },

View file

@ -1,13 +1,5 @@
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
<p>{{id}}-{{revision}}</p> <p>{{id}}-{{revision}}</p>
<p>{{created}}</p> <p>{{created}}</p>
<h1>{{title}}</h1> <h1>{{title}}</h1>
<p>{{body}}</p> <p>{{body}}</p>
</body>
</html>

9
templates/layout.html Normal file
View file

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>{{title}}</title>
</head>
<body>
{{{body}}}
</body>
</html>