Properly handle template inheritence

This commit is contained in:
Timothy Warren 2019-03-29 16:12:28 -04:00
parent 69444ebcaa
commit 7c67b11017
4 changed files with 41 additions and 47 deletions

View File

@ -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<HandlebarsEngine> {
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<T: ToJson>(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<String, Value>) -> Template {
template_data.insert("parent".to_string(), to_json(&"layout/base".to_owned()));
Template::new(name, template_data)
}
pub struct AssetsHandler;

View File

@ -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<HandlebarsEngine> {
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)

View File

@ -1,4 +1,7 @@
{{#*inline "page"}}
<main>
<h2>Index Page</h2>
<p>Testing rendering of internal content.</p>
</main>
</main>
{{/inline}}
{{~> (parent)~}}

View File

@ -1,5 +1,5 @@
{{> layout/top}}
{{> header}}
{{~ page}}
{{~> page}}
{{> footer}}
{{> layout/bottom}}