-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #103 from sunng87/feature/partial4.0
Handlebars 4.0 Partials
- Loading branch information
Showing
14 changed files
with
911 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use directives::DirectiveDef; | ||
use registry::Registry; | ||
use context::Context; | ||
use render::{RenderError, RenderContext, Directive}; | ||
|
||
#[derive(Clone, Copy)] | ||
pub struct InlineDirective; | ||
|
||
#[cfg(all(feature = "rustc_ser_type", not(feature = "serde_type")))] | ||
fn get_name<'a>(d: &'a Directive) -> Result<&'a str, RenderError> { | ||
d.param(0) | ||
.ok_or_else(|| RenderError::new("Param required for directive \"inline\"")) | ||
.and_then(|v| { | ||
v.value() | ||
.as_string() | ||
.ok_or_else(|| RenderError::new("inline name must be string")) | ||
}) | ||
} | ||
|
||
#[cfg(feature = "serde_type")] | ||
fn get_name<'a>(d: &'a Directive) -> Result<&'a str, RenderError> { | ||
d.param(0) | ||
.ok_or_else(|| RenderError::new("Param required for directive \"inline\"")) | ||
.and_then(|v| { | ||
v.value() | ||
.as_str() | ||
.ok_or_else(|| RenderError::new("inline name must be string")) | ||
}) | ||
} | ||
|
||
impl DirectiveDef for InlineDirective { | ||
fn call(&self, | ||
_: &Context, | ||
d: &Directive, | ||
_: &Registry, | ||
rc: &mut RenderContext) | ||
-> Result<(), RenderError> { | ||
let name = try!(get_name(d)); | ||
|
||
let template = try!(d.template() | ||
.ok_or_else(|| RenderError::new("inline should have a block"))); | ||
|
||
|
||
rc.set_partial(name.to_owned(), template.clone()); | ||
Ok(()) | ||
} | ||
} | ||
|
||
pub static INLINE_DIRECTIVE: InlineDirective = InlineDirective; | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use template::Template; | ||
use registry::Registry; | ||
use context::Context; | ||
use render::{RenderContext, Evalable}; | ||
use support::str::StringWriter; | ||
|
||
#[cfg(all(feature = "rustc_ser_type", not(feature = "serde_type")))] | ||
use serialize::json::Json; | ||
#[cfg(feature = "serde_type")] | ||
use serde_json::value::Value as Json; | ||
|
||
#[test] | ||
fn test_inline() { | ||
let t0 = | ||
Template::compile("{{#*inline \"hello\"}}the hello world inline partial.{{/inline}}" | ||
.to_string()) | ||
.ok() | ||
.unwrap(); | ||
|
||
let hbs = Registry::new(); | ||
|
||
let mut sw = StringWriter::new(); | ||
let mut rc = RenderContext::new(&mut sw); | ||
t0.elements[0].eval(&Context::wraps(&Json::Null), &hbs, &mut rc).unwrap(); | ||
|
||
assert!(rc.get_partial(&"hello".to_owned()).is_some()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use render::{RenderContext, RenderError, Directive}; | ||
use registry::Registry; | ||
use context::Context; | ||
|
||
pub use self::inline::INLINE_DIRECTIVE; | ||
|
||
/// Directive Definition | ||
/// | ||
/// Implement this trait to define your own decorators or directives | ||
pub trait DirectiveDef: Send + Sync { | ||
fn call(&self, | ||
ctx: &Context, | ||
d: &Directive, | ||
r: &Registry, | ||
rc: &mut RenderContext) | ||
-> Result<(), RenderError>; | ||
} | ||
|
||
/// implement DirectiveDef for bare function so we can use function as directive | ||
impl<F: Send + Sync + for<'a, 'b, 'c, 'd, 'e> Fn(&'a Context, &'b Directive, &'c Registry, &'d mut RenderContext) -> Result<(), RenderError>> DirectiveDef for F { | ||
fn call(&self, ctx: &Context, d: &Directive, r: &Registry, rc: &mut RenderContext) -> Result<(), RenderError>{ | ||
(*self)(ctx, d, r, rc) | ||
} | ||
} | ||
|
||
mod inline; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.