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 issue when upper block has block value #419

Merged
merged 2 commits into from Feb 20, 2021
Merged
Show file tree
Hide file tree
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
17 changes: 9 additions & 8 deletions src/context.rs
Expand Up @@ -78,19 +78,20 @@ fn parse_json_visitor<'a, 'reg>(
}
None => {
if path_context_depth > 0 {
if let Some(ref context_base_path) = block_contexts
let blk = block_contexts
.get(path_context_depth as usize)
.map(|blk| blk.base_path())
{
extend(&mut path_stack, context_base_path);
.or_else(|| block_contexts.front());

if let Some(base_value) = blk.and_then(|blk| blk.base_value()) {
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::LocalValue(path_stack, base_value)
} else {
// TODO: is this correct behaviour?
if let Some(ref base_path) = block_contexts.front().map(|blk| blk.base_path()) {
if let Some(base_path) = blk.map(|blk| blk.base_path()) {
extend(&mut path_stack, base_path);
}
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::AbsolutePath(path_stack)
}
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::AbsolutePath(path_stack)
} else if from_root {
merge_json_path(&mut path_stack, relative_path);
ResolvedPath::AbsolutePath(path_stack)
Expand Down
17 changes: 17 additions & 0 deletions src/partial.rs
Expand Up @@ -293,4 +293,21 @@ mod test {
let page = handlebars.render_template(&template3, &json!({})).unwrap();
assert_eq!("<outer><inner>Hello</inner></outer>", page);
}

#[test]
fn test_up_to_partial_level() {
let outer = r#"{{>inner name="fruit:" vegetables=fruits}}"#;
let inner = "{{#each vegetables}}{{../name}} {{this}},{{/each}}";

let data = json!({ "fruits": ["carrot", "tomato"] });

let mut handlebars = Registry::new();
handlebars.register_template_string("outer", outer).unwrap();
handlebars.register_template_string("inner", inner).unwrap();

assert_eq!(
handlebars.render("outer", &data).unwrap(),
"fruit: carrot,fruit: tomato,"
);
}
}