From 9a33195c424aa49384460ce66713cd21698c8611 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 9 May 2020 22:05:43 -0400 Subject: [PATCH] Generating the new treeX --- src/renderer/inline_partial_tree.rs | 2 +- src/renderer/renderer.rs | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/renderer/inline_partial_tree.rs b/src/renderer/inline_partial_tree.rs index f436552..7ae0bd9 100644 --- a/src/renderer/inline_partial_tree.rs +++ b/src/renderer/inline_partial_tree.rs @@ -4,7 +4,7 @@ use crate::parser::Template; use crate::parser::TemplateElement; use std::collections::HashMap; -struct InlinePartialTreeElement<'a> { +pub struct InlinePartialTreeElement<'a> { parent: Option<&'a InlinePartialTreeElement<'a>>, blocks: HashMap<&'a str, &'a Option>>, } diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 6cd6cb6..9df7f06 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -8,6 +8,8 @@ use crate::renderer::context_element::ContextElement; use crate::renderer::errors::CompileError; use crate::renderer::errors::RenderError; use crate::renderer::errors::WalkError; +use crate::renderer::inline_partial_tree::extract_inline_partials; +use crate::renderer::inline_partial_tree::InlinePartialTreeElement; use crate::renderer::parameters_context::ParametersContext; use crate::renderer::walking::walk_path; use std::collections::HashMap; @@ -52,6 +54,15 @@ impl<'a> DustRenderer<'a> { &'a self, name: &str, breadcrumbs: &Vec<&'a dyn ContextElement>, + ) -> Result { + self.render_template(name, breadcrumbs, None) + } + + fn render_template( + &'a self, + name: &str, + breadcrumbs: &Vec<&'a dyn ContextElement>, + blocks: Option<&'a InlinePartialTreeElement<'a>>, ) -> Result { let main_template = match self.templates.get(name) { Some(tmpl) => tmpl, @@ -59,6 +70,8 @@ impl<'a> DustRenderer<'a> { return Err(RenderError::TemplateNotFound(name.to_owned())); } }; + let extracted_inline_partials = extract_inline_partials(main_template); + let new_blocks = InlinePartialTreeElement::new(blocks, extracted_inline_partials); self.render_body(&main_template.contents, breadcrumbs) } @@ -173,13 +186,13 @@ impl<'a> DustRenderer<'a> { } DustTag::DTPartial(partial) => { if partial.params.is_empty() { - let rendered_content = self.render(&partial.name, breadcrumbs)?; + let rendered_content = self.render_template(&partial.name, breadcrumbs)?; return Ok(rendered_content); } else { let injected_context = ParametersContext::new(breadcrumbs, &partial.params); let mut new_breadcrumbs = breadcrumbs.clone(); new_breadcrumbs.insert(new_breadcrumbs.len() - 1, &injected_context); - let rendered_content = self.render(&partial.name, &new_breadcrumbs)?; + let rendered_content = self.render_template(&partial.name, &new_breadcrumbs)?; return Ok(rendered_content); } }