Skip to content

Commit 03ff9fb

Browse files
authored
Merge pull request #520 from sunng87/fix/hash-param-partial
Using both hash and param in partial include
2 parents 29fef48 + 4cdaa7d commit 03ff9fb

File tree

1 file changed

+33
-12
lines changed

1 file changed

+33
-12
lines changed

src/partial.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,18 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
7272
local_rc.dec_partial_block_depth();
7373
}
7474

75+
let mut block = None;
7576
let mut block_created = false;
7677

78+
// create context if param given
7779
if let Some(base_path) = d.param(0).and_then(|p| p.context_path()) {
7880
// path given, update base_path
79-
let mut block = BlockContext::new();
80-
*block.base_path_mut() = base_path.to_vec();
81-
block_created = true;
81+
let mut block_inner = BlockContext::new();
82+
*block_inner.base_path_mut() = base_path.to_vec();
83+
block = Some(block_inner);
84+
}
8285

83-
// clear blocks to prevent block params from parent
84-
// template to be leaked into partials
85-
local_rc.clear_blocks();
86-
local_rc.push_block(block);
87-
} else if !d.hash().is_empty() {
88-
let mut block = BlockContext::new();
86+
if !d.hash().is_empty() {
8987
// hash given, update base_value
9088
let hash_ctx = d
9189
.hash()
@@ -98,14 +96,24 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
9896
&hash_ctx,
9997
);
10098

101-
block.set_base_value(merged_context);
102-
block_created = true;
99+
if let Some(ref mut block_inner) = block {
100+
block_inner.set_base_value(merged_context);
101+
} else {
102+
let mut block_inner = BlockContext::new();
103+
block_inner.set_base_value(merged_context);
104+
block = Some(block_inner);
105+
}
106+
}
103107

108+
if let Some(block_inner) = block {
109+
// because block is moved here, we need another bool variable to track
110+
// its status for later cleanup
111+
block_created = true;
104112
// clear blocks to prevent block params from parent
105113
// template to be leaked into partials
106114
// see `test_partial_context_issue_495` for the case.
107115
local_rc.clear_blocks();
108-
local_rc.push_block(block);
116+
local_rc.push_block(block_inner);
109117
}
110118

111119
// @partial-block
@@ -270,6 +278,19 @@ mod test {
270278
);
271279
}
272280

281+
#[test]
282+
fn teset_partial_context_with_both_hash_and_param() {
283+
let mut hbs = Registry::new();
284+
hbs.register_template_string("one", "This is a test. {{> two this name=\"fred\" }}")
285+
.unwrap();
286+
hbs.register_template_string("two", "Lets test {{name}} and {{root_name}}")
287+
.unwrap();
288+
assert_eq!(
289+
"This is a test. Lets test fred and tom",
290+
hbs.render("one", &json!({"root_name": "tom"})).unwrap()
291+
);
292+
}
293+
273294
#[test]
274295
fn test_partial_subexpression_context_hash() {
275296
let mut hbs = Registry::new();

0 commit comments

Comments
 (0)