Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions src/partial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,24 @@ pub fn expand_partial<'reg: 'rc, 'rc>(
return Err(RenderError::new("Cannot include self in >"));
}

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

if let Some(t) = partial {
// clone to avoid lifetime issue
// FIXME refactor this to avoid
let mut local_rc = rc.clone();

// if tname == PARTIAL_BLOCK
let is_partial_block = tname == PARTIAL_BLOCK;

// add partial block depth there are consecutive partial
// blocks in the stack.
if is_partial_block {
local_rc.inc_partial_block_depth();
} else {
// depth cannot be lower than 0, which is guaranted in the
// `dec_partial_block_depth` method
local_rc.dec_partial_block_depth();
}

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

if is_partial_block {
local_rc.dec_partial_block_depth();
}

if d.template().is_some() {
local_rc.pop_partial_block();
}
Expand Down Expand Up @@ -297,7 +300,7 @@ mod test {
}

#[test]
fn test_nested_partials() {
fn test_nested_partial_block() {
let mut handlebars = Registry::new();
let template1 = "<outer>{{> @partial-block }}</outer>";
let template2 = "{{#> t1 }}<inner>{{> @partial-block }}</inner>{{/ t1 }}";
Expand Down Expand Up @@ -451,4 +454,41 @@ name: there
"#
);
}

#[test]
fn test_nested_partials() {
let mut hb = Registry::new();
hb.register_template_string("partial", "{{> @partial-block}}")
.unwrap();
hb.register_template_string(
"index",
r#"{{#>partial}}
Yo
{{#>partial}}
Yo 2
{{/partial}}
{{/partial}}"#,
)
.unwrap();
assert_eq!(
r#" Yo
Yo 2
"#,
hb.render("index", &()).unwrap()
);

hb.register_template_string("partial2", "{{> @partial-block}}")
.unwrap();
let r2 = hb
.render_template(
r#"{{#> partial}}
{{#> partial2}}
:(
{{/partial2}}
{{/partial}}"#,
&(),
)
.unwrap();
assert_eq!(":(\n", r2);
}
}
6 changes: 5 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ impl<'reg: 'rc, 'rc> RenderContext<'reg, 'rc> {
}

pub(crate) fn dec_partial_block_depth(&mut self) {
self.inner_mut().partial_block_depth -= 1;
let depth = &mut self.inner_mut().partial_block_depth;
if *depth > 0 {
*depth -= 1;
}
}

/// Remove a registered partial
Expand Down Expand Up @@ -275,6 +278,7 @@ impl<'reg, 'rc> fmt::Debug for RenderContextInner<'reg, 'rc> {
f.debug_struct("RenderContextInner")
.field("partials", &self.partials)
.field("partial_block_stack", &self.partial_block_stack)
.field("partial_block_depth", &self.partial_block_depth)
.field("root_template", &self.root_template)
.field("current_template", &self.current_template)
.field("disable_escape", &self.disable_escape)
Expand Down