Properly handle template inheritence
This commit is contained in:
parent
69444ebcaa
commit
7c67b11017
@ -1,56 +1,18 @@
|
|||||||
use handlebars::{Handlebars, no_escape};
|
|
||||||
use handlebars_iron::{DirectorySource, HandlebarsEngine};
|
|
||||||
use handlebars_iron::handlebars::to_json;
|
use handlebars_iron::handlebars::to_json;
|
||||||
use handlebars_iron::Template;
|
use handlebars_iron::Template;
|
||||||
use iron::prelude::{Request, Response};
|
use iron::prelude::{Request, Response};
|
||||||
use iron::{IronResult, Set, status};
|
use iron::{IronResult, Set, status};
|
||||||
use mount::Mount;
|
use mount::Mount;
|
||||||
use serde::ser::Serialize as ToJson;
|
use serde::ser::Serialize as ToJson;
|
||||||
use serde_json::value::Map;
|
use serde_json::value::{Map, Value};
|
||||||
use staticfile::Static;
|
use staticfile::Static;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
pub fn template_engine() -> Arc<HandlebarsEngine> {
|
pub fn render_page(name: &str, mut template_data: Map<String, Value>) -> Template {
|
||||||
let views_ext = ".hbs";
|
template_data.insert("parent".to_string(), to_json(&"layout/base".to_owned()));
|
||||||
let views_path = "./src/views";
|
Template::new(name, template_data)
|
||||||
|
|
||||||
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 struct AssetsHandler;
|
pub struct AssetsHandler;
|
||||||
|
33
src/main.rs
33
src/main.rs
@ -2,9 +2,38 @@
|
|||||||
#[macro_use] extern crate mime;
|
#[macro_use] extern crate mime;
|
||||||
|
|
||||||
use env_logger;
|
use env_logger;
|
||||||
|
use handlebars::Handlebars;
|
||||||
|
use handlebars_iron::{DirectorySource, HandlebarsEngine};
|
||||||
use iron::prelude::Iron;
|
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() {
|
fn main() {
|
||||||
env_logger::init();
|
env_logger::init();
|
||||||
@ -14,7 +43,7 @@ fn main() {
|
|||||||
let bind_addr = format!("localhost:{}", port);
|
let bind_addr = format!("localhost:{}", port);
|
||||||
|
|
||||||
let mut chain = chain::init();
|
let mut chain = chain::init();
|
||||||
let templating_engine = handlers::template_engine().clone();
|
let templating_engine = template_engine();
|
||||||
chain.link_after(templating_engine);
|
chain.link_after(templating_engine);
|
||||||
|
|
||||||
let _server_guard = Iron::new(chain)
|
let _server_guard = Iron::new(chain)
|
||||||
|
@ -1,4 +1,7 @@
|
|||||||
|
{{#*inline "page"}}
|
||||||
<main>
|
<main>
|
||||||
<h2>Index Page</h2>
|
<h2>Index Page</h2>
|
||||||
<p>Testing rendering of internal content.</p>
|
<p>Testing rendering of internal content.</p>
|
||||||
</main>
|
</main>
|
||||||
|
{{/inline}}
|
||||||
|
{{~> (parent)~}}
|
@ -1,5 +1,5 @@
|
|||||||
{{> layout/top}}
|
{{> layout/top}}
|
||||||
{{> header}}
|
{{> header}}
|
||||||
{{~ page}}
|
{{~> page}}
|
||||||
{{> footer}}
|
{{> footer}}
|
||||||
{{> layout/bottom}}
|
{{> layout/bottom}}
|
Loading…
Reference in New Issue
Block a user