Skip to content

Commit

Permalink
(fix) added parent path to local path root [#93]
Browse files Browse the repository at this point in the history
Signed-off-by: Ning Sun <[email protected]>
  • Loading branch information
sunng87 committed Jul 26, 2016
1 parent d633621 commit baf077b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
26 changes: 25 additions & 1 deletion src/helpers/helper_each.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ impl HelperDef for EachHelper {

rc.promote_local_vars();
if let Some(path_root) = value.path_root() {
rc.set_local_path_root(path_root.to_owned());
let local_path_root = format!("{}/{}", path, path_root);
rc.set_local_path_root(local_path_root);
}

debug!("each value {:?}", value.value());
Expand Down Expand Up @@ -101,6 +102,10 @@ impl HelperDef for EachHelper {
let path = rc.get_path().clone();

rc.promote_local_vars();
if let Some(path_root) = value.path_root() {
let local_path_root = format!("{}/{}", path, path_root);
rc.set_local_path_root(local_path_root);
}

debug!("each value {:?}", value.value());
let rendered = match value.value() {
Expand Down Expand Up @@ -215,6 +220,25 @@ mod test {
assert_eq!(r1.ok().unwrap(), " d=100 b=99 d=200 b=99 ".to_string());
}

#[test]
#[cfg(all(feature = "rustc_ser_type", not(feature = "serde_type")))]
fn test_nested_each_with_parent() {

let json_str = r#"{"a": [{"b": [{"d": 100}], "c": 200}]}"#;

let data = Json::from_str(json_str).unwrap();
let t0 = Template::compile("{{#each a}}{{#each b}}{{d}}:{{../c}}{{/each}}{{/each}}"
.to_string())
.ok()
.unwrap();

let mut handlebars = Registry::new();
handlebars.register_template("t0", t0);

let r1 = handlebars.render("t0", &data);
assert_eq!(r1.ok().unwrap(), "100:200".to_string());
}

#[test]
fn test_nested_array() {
let t0 = Template::compile("{{#each this.[0]}}{{this}}{{/each}}".to_owned()).ok().unwrap();
Expand Down
3 changes: 0 additions & 3 deletions src/helpers/helper_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ impl HelperDef for IfHelper {
.ok_or_else(|| RenderError::new("Param not found for helper \"if\"")));

let mut value = param.value().is_truthy();
if let Some(root_path) = param.path_root() {
rc.set_local_path_root(root_path.to_owned());
}

if !self.positive {
value = !value;
Expand Down

0 comments on commit baf077b

Please sign in to comment.