Split layout out from rendering responses
This commit is contained in:
parent
35e7bad7d2
commit
4a37a91ece
3 changed files with 22 additions and 9 deletions
14
src/site.rs
14
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()
|
||||
},
|
||||
|
|
|
@ -1,13 +1,5 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{title}}</title>
|
||||
</head>
|
||||
<body>
|
||||
<p>{{id}}-{{revision}}</p>
|
||||
<p>{{created}}</p>
|
||||
|
||||
<h1>{{title}}</h1>
|
||||
<p>{{body}}</p>
|
||||
</body>
|
||||
</html>
|
||||
|
|
9
templates/layout.html
Normal file
9
templates/layout.html
Normal file
|
@ -0,0 +1,9 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>{{title}}</title>
|
||||
</head>
|
||||
<body>
|
||||
{{{body}}}
|
||||
</body>
|
||||
</html>
|
Loading…
Reference in a new issue