diff --git a/src/handlers.rs b/src/handlers.rs index 3df3536..756d516 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -1,56 +1,18 @@ -use handlebars::{Handlebars, no_escape}; -use handlebars_iron::{DirectorySource, HandlebarsEngine}; + use handlebars_iron::handlebars::to_json; use handlebars_iron::Template; use iron::prelude::{Request, Response}; use iron::{IronResult, Set, status}; use mount::Mount; use serde::ser::Serialize as ToJson; -use serde_json::value::Map; +use serde_json::value::{Map, Value}; use staticfile::Static; use std::error::Error; use std::path::Path; -use std::sync::Arc; -pub fn template_engine() -> Arc { - let views_ext = ".hbs"; - let views_path = "./src/views"; - - let mut hbs = Handlebars::new(); - - // @TODO Find better solution for rendering included template file - hbs.register_escape_fn(no_escape); - - - let mut hbse = HandlebarsEngine::from(hbs); - hbse.add(Box::new( - DirectorySource::new(views_path, views_ext) - )); - - if let Err(r) = hbse.reload() { - panic!("{:?}", r.description()); - } - - let hbse_ref = Arc::new(hbse); - if cfg!(debug_assertions) { - println!("Templates are being watched."); - use handlebars_iron::Watchable; - hbse_ref.watch(views_path); - } - - hbse_ref -} - -pub fn render_page(name: &str, template_data: T) -> Template { - let template_engine = template_engine().clone(); - let template_registry = template_engine.handlebars_mut(); - - let page = template_registry.render(name, &template_data).unwrap(); - - let mut data = Map::new(); - data.insert("page".to_string(), to_json(page)); - - Template::new("layout/base", data) +pub fn render_page(name: &str, mut template_data: Map) -> Template { + template_data.insert("parent".to_string(), to_json(&"layout/base".to_owned())); + Template::new(name, template_data) } pub struct AssetsHandler; diff --git a/src/main.rs b/src/main.rs index a042743..60a3f97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,9 +2,38 @@ #[macro_use] extern crate mime; use env_logger; +use handlebars::Handlebars; +use handlebars_iron::{DirectorySource, HandlebarsEngine}; use iron::prelude::Iron; +use std::error::Error; +use std::sync::Arc; -use media_collection_crud::{chain, handlers, db}; +use media_collection_crud::{chain, db}; + +fn template_engine() -> Arc { + let views_ext = ".hbs"; + let views_path = "./src/views"; + + let hbs = Handlebars::new(); + + let mut hbse = HandlebarsEngine::from(hbs); + hbse.add(Box::new( + DirectorySource::new(views_path, views_ext) + )); + + if let Err(r) = hbse.reload() { + panic!("{:?}", r.description()); + } + + let hbse_ref = Arc::new(hbse); + if cfg!(debug_assertions) { + println!("Templates are being watched."); + use handlebars_iron::Watchable; + hbse_ref.watch(views_path); + } + + hbse_ref +} fn main() { env_logger::init(); @@ -14,7 +43,7 @@ fn main() { let bind_addr = format!("localhost:{}", port); let mut chain = chain::init(); - let templating_engine = handlers::template_engine().clone(); + let templating_engine = template_engine(); chain.link_after(templating_engine); let _server_guard = Iron::new(chain) diff --git a/src/views/index.hbs b/src/views/index.hbs index 2323256..6e2087f 100644 --- a/src/views/index.hbs +++ b/src/views/index.hbs @@ -1,4 +1,7 @@ +{{#*inline "page"}}

Index Page

Testing rendering of internal content.

-
\ No newline at end of file + +{{/inline}} +{{~> (parent)~}} \ No newline at end of file diff --git a/src/views/layout/base.hbs b/src/views/layout/base.hbs index a80aef0..780bd5b 100644 --- a/src/views/layout/base.hbs +++ b/src/views/layout/base.hbs @@ -1,5 +1,5 @@ {{> layout/top}} {{> header}} - {{~ page}} + {{~> page}} {{> footer}} {{> layout/bottom}} \ No newline at end of file