From baf077b9cc3a0cd968f67389383820ffe3bf2f0e Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Tue, 26 Jul 2016 21:46:18 +0800 Subject: [PATCH] (fix) added parent path to local path root [#93] Signed-off-by: Ning Sun --- src/helpers/helper_each.rs | 26 +++++++++++++++++++++++++- src/helpers/helper_if.rs | 3 --- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/src/helpers/helper_each.rs b/src/helpers/helper_each.rs index 28ecc8031..ebd7ef99a 100644 --- a/src/helpers/helper_each.rs +++ b/src/helpers/helper_each.rs @@ -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()); @@ -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() { @@ -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(); diff --git a/src/helpers/helper_if.rs b/src/helpers/helper_if.rs index 0d12cf8a6..bd73d2dfe 100644 --- a/src/helpers/helper_if.rs +++ b/src/helpers/helper_if.rs @@ -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;