Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix case where with is used inside an each with nested data access #97

Merged
merged 1 commit into from
Aug 14, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/helpers/helper_with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ impl HelperDef for WithHelper {

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

let not_empty = param.value().is_truthy();
let template = if not_empty {
Expand Down Expand Up @@ -123,4 +127,51 @@ mod test {
let r2 = handlebars.render("t2", &person);
assert_eq!(r2.ok().unwrap(), "China".to_string());
}

#[test]
fn test_with_in_each(){
let addr = Address {
city: "Beijing".to_string(),
country: "China".to_string(),
};

let person = Person {
name: "Ning Sun".to_string(),
age: 27,
addr: addr,
titles: vec!["programmer".to_string(), "cartographier".to_string()],
};

let addr2 = Address {
city: "Beijing".to_string(),
country: "China".to_string(),
};

let person2 = Person {
name: "Ning Sun".to_string(),
age: 27,
addr: addr2,
titles: vec!["programmer".to_string(), "cartographier".to_string()],
};

let people = vec![person, person2];

let t0 = Template::compile("{{#each this}}{{#with addr}}{{city}}{{/with}}{{/each}}".to_string()).ok().unwrap();
let t1 = Template::compile("{{#each this}}{{#with addr}}{{../age}}{{/with}}{{/each}}".to_string()).ok().unwrap();
let t2 = Template::compile("{{#each this}}{{#with addr}}{{@../index}}{{/with}}{{/each}}".to_string()).ok().unwrap();

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

let r0 = handlebars.render("t0", &people);
assert_eq!(r0.ok().unwrap(), "BeijingBeijing".to_string());

let r1 = handlebars.render("t1", &people);
assert_eq!(r1.ok().unwrap(), "2727".to_string());

let r2 = handlebars.render("t2", &people);
assert_eq!(r2.ok().unwrap(), "01".to_string());
}
}