make Context public

This commit is contained in:
Jacob Brown 2017-07-21 15:53:17 -05:00
parent 6d3cd374ac
commit a2894b10ab
2 changed files with 18 additions and 14 deletions

View File

@ -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;

View File

@ -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`.