Passing the block tree through the render functions.

This commit is contained in:
Tom Alexander 2020-05-09 22:29:58 -04:00
parent 9a33195c42
commit 6837fba489
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE

View File

@ -72,13 +72,14 @@ impl<'a> DustRenderer<'a> {
}; };
let extracted_inline_partials = extract_inline_partials(main_template); let extracted_inline_partials = extract_inline_partials(main_template);
let new_blocks = InlinePartialTreeElement::new(blocks, extracted_inline_partials); let new_blocks = InlinePartialTreeElement::new(blocks, extracted_inline_partials);
self.render_body(&main_template.contents, breadcrumbs) self.render_body(&main_template.contents, breadcrumbs, &new_blocks)
} }
fn render_body( fn render_body(
&'a self, &'a self,
body: &'a Body, body: &'a Body,
breadcrumbs: &Vec<&'a dyn ContextElement>, breadcrumbs: &Vec<&'a dyn ContextElement>,
blocks: &'a InlinePartialTreeElement<'a>,
) -> Result<String, RenderError> { ) -> Result<String, RenderError> {
let mut output = String::new(); let mut output = String::new();
for elem in &body.elements { for elem in &body.elements {
@ -86,7 +87,7 @@ impl<'a> DustRenderer<'a> {
TemplateElement::TEIgnoredWhitespace(_) => {} TemplateElement::TEIgnoredWhitespace(_) => {}
TemplateElement::TESpan(span) => output.push_str(span.contents), TemplateElement::TESpan(span) => output.push_str(span.contents),
TemplateElement::TETag(dt) => { TemplateElement::TETag(dt) => {
output.push_str(&self.render_tag(dt, breadcrumbs)?); output.push_str(&self.render_tag(dt, breadcrumbs, blocks)?);
} }
} }
} }
@ -97,6 +98,7 @@ impl<'a> DustRenderer<'a> {
&'a self, &'a self,
tag: &'a DustTag, tag: &'a DustTag,
breadcrumbs: &Vec<&'a dyn ContextElement>, breadcrumbs: &Vec<&'a dyn ContextElement>,
blocks: &'a InlinePartialTreeElement<'a>,
) -> Result<String, RenderError> { ) -> Result<String, RenderError> {
match tag { match tag {
DustTag::DTComment(_comment) => (), DustTag::DTComment(_comment) => (),
@ -133,7 +135,7 @@ impl<'a> DustRenderer<'a> {
// original context before walking the path as // original context before walking the path as
// the context for rendering the else block // the context for rendering the else block
return match &container.else_contents { return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs, blocks),
None => Ok("".to_owned()), None => Ok("".to_owned()),
}; };
} else { } else {
@ -145,7 +147,7 @@ impl<'a> DustRenderer<'a> {
.map(|array_elem| { .map(|array_elem| {
let mut new_breadcumbs = breadcrumbs.clone(); let mut new_breadcumbs = breadcrumbs.clone();
new_breadcumbs.push(array_elem); new_breadcumbs.push(array_elem);
self.render_body(&body, &new_breadcumbs) self.render_body(&body, &new_breadcumbs, blocks)
}) })
.collect(); .collect();
let rendered_slice: &[String] = &rendered_results?; let rendered_slice: &[String] = &rendered_results?;
@ -159,13 +161,13 @@ impl<'a> DustRenderer<'a> {
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val); let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
if loop_elements.is_empty() { if loop_elements.is_empty() {
return match &container.else_contents { return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs, blocks),
None => Ok("".to_owned()), None => Ok("".to_owned()),
}; };
} else { } else {
return match &container.contents { return match &container.contents {
None => Ok("".to_owned()), None => Ok("".to_owned()),
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs, blocks),
}; };
} }
} }
@ -174,25 +176,27 @@ impl<'a> DustRenderer<'a> {
let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val); let loop_elements: Vec<&dyn ContextElement> = self.get_loop_elements(val);
if !loop_elements.is_empty() { if !loop_elements.is_empty() {
return match &container.else_contents { return match &container.else_contents {
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs, blocks),
None => Ok("".to_owned()), None => Ok("".to_owned()),
}; };
} else { } else {
return match &container.contents { return match &container.contents {
None => Ok("".to_owned()), None => Ok("".to_owned()),
Some(body) => self.render_body(&body, breadcrumbs), Some(body) => self.render_body(&body, breadcrumbs, blocks),
}; };
} }
} }
DustTag::DTPartial(partial) => { DustTag::DTPartial(partial) => {
if partial.params.is_empty() { if partial.params.is_empty() {
let rendered_content = self.render_template(&partial.name, breadcrumbs)?; let rendered_content =
self.render_template(&partial.name, breadcrumbs, Some(blocks))?;
return Ok(rendered_content); return Ok(rendered_content);
} else { } else {
let injected_context = ParametersContext::new(breadcrumbs, &partial.params); let injected_context = ParametersContext::new(breadcrumbs, &partial.params);
let mut new_breadcrumbs = breadcrumbs.clone(); let mut new_breadcrumbs = breadcrumbs.clone();
new_breadcrumbs.insert(new_breadcrumbs.len() - 1, &injected_context); new_breadcrumbs.insert(new_breadcrumbs.len() - 1, &injected_context);
let rendered_content = self.render_template(&partial.name, &new_breadcrumbs)?; let rendered_content =
self.render_template(&partial.name, &new_breadcrumbs, Some(blocks))?;
return Ok(rendered_content); return Ok(rendered_content);
} }
} }