Skip to content

Commit 4f3def3

Browse files
committed
fix: corrected partial_block_depth mechanism
Fixes #487 We can only increase `patial_block_depth` for scenarios where consecutive `@partial-block` are rendered.
1 parent bc17d7e commit 4f3def3

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/partial.rs

Lines changed: 12 additions & 8 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 }}";
@@ -453,7 +456,7 @@ name: there
453456
}
454457

455458
#[test]
456-
fn test_nested_partial() {
459+
fn test_nested_partials() {
457460
let mut hb = Registry::new();
458461
hb.register_template_string("partial", "{{> @partial-block}}")
459462
.unwrap();
@@ -469,7 +472,8 @@ name: there
469472
.unwrap();
470473
assert_eq!(
471474
r#" Yo
472-
Yo 2"#,
475+
Yo 2
476+
"#,
473477
hb.render("index", &()).unwrap()
474478
);
475479
}

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)