Skip to content

Commit a93a327

Browse files
authored
Merge pull request #488 from sunng87/fix/nested-partial-with-partial-block
Fix partial block referencing in nested partials
2 parents 44c1e5a + a27ee58 commit a93a327

File tree

2 files changed

+51
-7
lines changed

2 files changed

+51
-7
lines changed

src/partial.rs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,24 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
5252
return Err(RenderError::new("Cannot include self in >"));
5353
}
5454

55-
// if tname == PARTIAL_BLOCK
5655
let partial = find_partial(rc, r, d, tname)?;
5756

5857
if let Some(t) = partial {
5958
// clone to avoid lifetime issue
6059
// FIXME refactor this to avoid
6160
let mut local_rc = rc.clone();
61+
62+
// if tname == PARTIAL_BLOCK
6263
let is_partial_block = tname == PARTIAL_BLOCK;
6364

65+
// add partial block depth there are consecutive partial
66+
// blocks in the stack.
6467
if is_partial_block {
6568
local_rc.inc_partial_block_depth();
69+
} else {
70+
// depth cannot be lower than 0, which is guaranted in the
71+
// `dec_partial_block_depth` method
72+
local_rc.dec_partial_block_depth();
6673
}
6774

6875
let mut block_created = false;
@@ -103,10 +110,6 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
103110
local_rc.pop_block();
104111
}
105112

106-
if is_partial_block {
107-
local_rc.dec_partial_block_depth();
108-
}
109-
110113
if d.template().is_some() {
111114
local_rc.pop_partial_block();
112115
}
@@ -297,7 +300,7 @@ mod test {
297300
}
298301

299302
#[test]
300-
fn test_nested_partials() {
303+
fn test_nested_partial_block() {
301304
let mut handlebars = Registry::new();
302305
let template1 = "<outer>{{> @partial-block }}</outer>";
303306
let template2 = "{{#> t1 }}<inner>{{> @partial-block }}</inner>{{/ t1 }}";
@@ -451,4 +454,41 @@ name: there
451454
"#
452455
);
453456
}
457+
458+
#[test]
459+
fn test_nested_partials() {
460+
let mut hb = Registry::new();
461+
hb.register_template_string("partial", "{{> @partial-block}}")
462+
.unwrap();
463+
hb.register_template_string(
464+
"index",
465+
r#"{{#>partial}}
466+
Yo
467+
{{#>partial}}
468+
Yo 2
469+
{{/partial}}
470+
{{/partial}}"#,
471+
)
472+
.unwrap();
473+
assert_eq!(
474+
r#" Yo
475+
Yo 2
476+
"#,
477+
hb.render("index", &()).unwrap()
478+
);
479+
480+
hb.register_template_string("partial2", "{{> @partial-block}}")
481+
.unwrap();
482+
let r2 = hb
483+
.render_template(
484+
r#"{{#> partial}}
485+
{{#> partial2}}
486+
:(
487+
{{/partial2}}
488+
{{/partial}}"#,
489+
&(),
490+
)
491+
.unwrap();
492+
assert_eq!(":(\n", r2);
493+
}
454494
}

src/render.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,10 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
190190
}
191191

192192
pub(crate) fn dec_partial_block_depth(&mut self) {
193-
self.inner_mut().partial_block_depth -= 1;
193+
let depth = &mut self.inner_mut().partial_block_depth;
194+
if *depth > 0 {
195+
*depth -= 1;
196+
}
194197
}
195198

196199
/// Remove a registered partial
@@ -275,6 +278,7 @@ impl<'reg, 'rc> fmt::Debug for RenderContextInner<'reg, 'rc> {
275278
f.debug_struct("RenderContextInner")
276279
.field("partials", &self.partials)
277280
.field("partial_block_stack", &self.partial_block_stack)
281+
.field("partial_block_depth", &self.partial_block_depth)
278282
.field("root_template", &self.root_template)
279283
.field("current_template", &self.current_template)
280284
.field("disable_escape", &self.disable_escape)

0 commit comments

Comments
 (0)