Skip to content

Commit 1b7522b

Browse files
authored
Merge pull request #496 from sunng87/fix/partial-context
Fix block param scope leaked into partials
2 parents 09e9a4f + 8066a73 commit 1b7522b

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed

src/context.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ impl Context {
173173
// always use absolute at the moment until we get base_value lifetime issue fixed
174174
let resolved_visitor = parse_json_visitor(relative_path, block_contexts, true);
175175

176-
// debug logging
177-
debug!("Accessing context value: {:?}", resolved_visitor);
178-
179176
match resolved_visitor {
180177
ResolvedPath::AbsolutePath(paths) => {
181178
let mut ptr = Some(self.data());

src/partial.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
7979
let mut block = BlockContext::new();
8080
*block.base_path_mut() = base_path.to_vec();
8181
block_created = true;
82+
83+
// clear blocks to prevent block params from parent
84+
// template to be leaked into partials
85+
local_rc.clear_blocks();
8286
local_rc.push_block(block);
8387
} else if !d.hash().is_empty() {
8488
let mut block = BlockContext::new();
@@ -93,8 +97,14 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
9397
local_rc.evaluate2(ctx, &Path::current())?.as_json(),
9498
&hash_ctx,
9599
);
100+
96101
block.set_base_value(merged_context);
97102
block_created = true;
103+
104+
// clear blocks to prevent block params from parent
105+
// template to be leaked into partials
106+
// see `test_partial_context_issue_495` for the case.
107+
local_rc.clear_blocks();
98108
local_rc.push_block(block);
99109
}
100110

@@ -491,4 +501,45 @@ name: there
491501
.unwrap();
492502
assert_eq!(":(\n", r2);
493503
}
504+
505+
#[test]
506+
fn test_partial_context_issue_495() {
507+
let mut hb = Registry::new();
508+
hb.register_template_string(
509+
"t1",
510+
r#"{{~#*inline "displayName"~}}
511+
Template:{{name}}
512+
{{/inline}}
513+
{{#each data as |name|}}
514+
Name:{{name}}
515+
{{>displayName name="aaaa"}}
516+
{{/each}}"#,
517+
)
518+
.unwrap();
519+
520+
hb.register_template_string(
521+
"t1",
522+
r#"{{~#*inline "displayName"~}}
523+
Template:{{this}}
524+
{{/inline}}
525+
{{#each data as |name|}}
526+
Name:{{name}}
527+
{{>displayName}}
528+
{{/each}}"#,
529+
)
530+
.unwrap();
531+
532+
let data = json!({
533+
"data": ["hudel", "test"]
534+
});
535+
536+
assert_eq!(
537+
r#"Name:hudel
538+
Template:hudel
539+
Name:test
540+
Template:test
541+
"#,
542+
hb.render("t1", &data).unwrap()
543+
);
544+
}
494545
}

src/render.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
101101
self.blocks.pop_front();
102102
}
103103

104+
pub(crate) fn clear_blocks(&mut self) {
105+
self.blocks.clear();
106+
}
107+
104108
/// Borrow a reference to current block context
105109
pub fn block(&self) -> Option<&BlockContext<'reg>> {
106110
self.blocks.front()

0 commit comments

Comments
 (0)