diff --git a/contrib/src/lib.rs b/contrib/src/lib.rs index 74237adf..7764b16e 100644 --- a/contrib/src/lib.rs +++ b/contrib/src/lib.rs @@ -67,7 +67,7 @@ pub use msgpack::{MsgPack, MsgPackError}; mod templates; #[cfg(feature = "templates")] -pub use templates::Template; +pub use templates::{Template, Context as TemplateContext}; #[cfg(feature = "uuid")] mod uuid; diff --git a/contrib/src/templates/mod.rs b/contrib/src/templates/mod.rs index 8e312f2a..76757cd4 100644 --- a/contrib/src/templates/mod.rs +++ b/contrib/src/templates/mod.rs @@ -8,7 +8,6 @@ mod engine; mod context; use self::engine::{Engine, Engines}; -use self::context::Context; use self::serde::Serialize; use self::serde_json::{Value, to_value}; use self::glob::glob; @@ -16,12 +15,14 @@ use self::glob::glob; use std::borrow::Cow; use std::path::{Path, PathBuf}; -use rocket::State; +use rocket::{Config, State}; use rocket::request::Request; use rocket::fairing::{Fairing, AdHoc}; use rocket::response::{self, Content, Responder}; use rocket::http::{ContentType, Status}; +pub use self::context::Context; + const DEFAULT_TEMPLATE_DIR: &'static str = "templates"; /// The Template type implements generic support for template rendering in @@ -158,23 +159,26 @@ impl Template { /// ``` pub fn fairing() -> impl Fairing { AdHoc::on_attach(|rocket| { - let mut template_root = rocket.config().root().join(DEFAULT_TEMPLATE_DIR); - match rocket.config().get_str("template_dir") { - Ok(dir) => template_root = rocket.config().root().join(dir), - Err(ref e) if !e.is_not_found() => { - e.pretty_print(); - warn_!("Using default directory '{:?}'", template_root); - } - Err(_) => { } - }; - - match Context::initialize(template_root) { + match Context::initialize(Self::root(rocket.config())) { Some(ctxt) => Ok(rocket.manage(ctxt)), None => Err(rocket) } }) } + pub fn root(config: &Config) -> PathBuf { + let mut template_root = config.root().join(DEFAULT_TEMPLATE_DIR); + match config.get_str("template_dir") { + Ok(dir) => template_root = config.root().join(dir), + Err(ref e) if !e.is_not_found() => { + e.pretty_print(); + warn_!("Using default directory '{:?}'", template_root); + } + Err(_) => { } + }; + template_root + } + /// Render the template named `name` with the context `context`. The /// `context` can be of any type that implements `Serialize`. This is /// typically a `HashMap` or a custom `struct`.