Use handlebars templates

This commit is contained in:
Tiffany Bennett 2016-10-03 23:52:39 -04:00
parent 3067a1bc20
commit 4236c9c8a3
3 changed files with 29 additions and 18 deletions

View file

@ -16,3 +16,5 @@ features = ["hyper", "sandbox"]
iron = "0.4.0"
router = "0.4.0"
params = "0.4.0"
handlebars = "0.20.0"
handlebars-iron = "0.18.0"

View file

@ -6,31 +6,18 @@ extern crate rink;
extern crate iron;
extern crate router;
extern crate params;
extern crate handlebars;
extern crate handlebars_iron;
use iron::prelude::*;
use iron::status;
use router::Router;
use iron::headers;
use iron::modifiers::Header;
use iron::mime::Mime;
static TEMPLATE: &'static str = r##"
<html>
<head>
<title>Rink</title>
</head>
<body>
<form action="/api" method="POST" target="_self">
<input name="query" type="text" />
<input type="submit" />
</form>
</body>
</html>
"##;
use handlebars_iron::{HandlebarsEngine, DirectorySource, Template};
fn root(_req: &mut Request) -> IronResult<Response> {
let mime: Mime = "text/html".parse().unwrap();
Ok(Response::with((status::Ok, TEMPLATE, mime)))
Ok(Response::with(Template::new("index", ())))
}
fn api(req: &mut Request) -> IronResult<Response> {
@ -54,5 +41,16 @@ fn main() {
let mut router = Router::new();
router.get("/", root, "root");
router.get("/api", api, "api");
Iron::new(router).http("localhost:8000").unwrap();
let mut chain = Chain::new(router);
let mut hbse = HandlebarsEngine::new();
hbse.add(Box::new(DirectorySource::new("./templates/", ".hbs")));
// load templates from all registered sources
if let Err(r) = hbse.reload() {
panic!("{}", r);
}
chain.link_after(hbse);
Iron::new(chain).http("localhost:8000").unwrap();
}

11
web/templates/index.hbs Normal file
View file

@ -0,0 +1,11 @@
<html>
<head>
<title>Rink</title>
</head>
<body>
<form action="/api" method="GET" target="_self">
<input name="query" type="text" />
<input type="submit" />
</form>
</body>
</html>