From f04e84dc3188e4b34850d4797cc6e1e1303fc95a Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Mon, 25 May 2020 23:28:49 -0400 Subject: [PATCH] Attempted to move section over to the section-specific new breadcrumbs function. I think the problem is, the index context needs to be a higher priority than the new context element, but when referencing data with `{.}` the new context element needs to take priority. I could either combine entries in the context tree so that variables like $idx and $len live side-by-side with real context elements, or try to implement a way to skip over index context elements when walking up the tree. --- .../explicit_context_setting/main.dust | 16 +++++ src/renderer/renderer.rs | 65 ++++++++----------- 2 files changed, 44 insertions(+), 37 deletions(-) diff --git a/js/test_cases/explicit_context_setting/main.dust b/js/test_cases/explicit_context_setting/main.dust index 2997d29..7313042 100644 --- a/js/test_cases/explicit_context_setting/main.dust +++ b/js/test_cases/explicit_context_setting/main.dust @@ -327,6 +327,22 @@ Section vs Partial priority{~n} {/some_global} {>priority:explicit pet="snake"/} +Section vs Exists priority{~n} +=========================={~n} +{?some_global:explicit pet="snake"} + {pet}{~n} +{/some_global} +{>priority:explicit pet="snake"/} + +Section vs NotExists priority{~n} +=========================={~n} +{^some_global:explicit pet="snake"} + MAIN {pet}{~n} +{:else} + ELSE {pet}{~n} +{/some_global} +{>priority:explicit pet="snake"/} + Section vs Partial priority Failed Walk{~n} ======================================={~n} {#doesnotexist:explicit pet="snake"} diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index d73330c..0de9263 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -165,13 +165,14 @@ impl<'a> DustRenderer<'a> { } } DustTag::DTSection(container) => { + let injected_context = ParametersContext::new(breadcrumbs, &container.params); let val = walk_path(breadcrumbs, &container.path.keys); match val { Err(WalkError::CantWalk) => { - let new_breadcrumbs = Self::new_breadcrumbs( - breadcrumbs, + let new_breadcrumbs = Self::new_breadcrumbs_section( breadcrumbs, None, + Some(&injected_context), &container.explicit_context, None, ); @@ -194,10 +195,10 @@ impl<'a> DustRenderer<'a> { final_val.get_loop_elements(); if loop_elements.is_empty() { // Scalar value - let new_breadcrumbs = Self::new_breadcrumbs( - breadcrumbs, + let new_breadcrumbs = Self::new_breadcrumbs_section( breadcrumbs, None, + Some(&injected_context), &container.explicit_context, Some(final_val), ); @@ -214,15 +215,16 @@ impl<'a> DustRenderer<'a> { .into_iter() .enumerate() .map(|(i, array_elem)| { - let injected_context = + let index_context = IterationContext::new(i, total_length); - let new_breadcrumbs = Self::new_breadcrumbs( - breadcrumbs, - breadcrumbs, - Some(&injected_context), - &container.explicit_context, - Some(array_elem), - ); + let new_breadcrumbs = + Self::new_breadcrumbs_section( + breadcrumbs, + Some(&index_context), + Some(&injected_context), + &container.explicit_context, + Some(array_elem), + ); self.render_body( &body, new_breadcrumbs @@ -242,10 +244,10 @@ impl<'a> DustRenderer<'a> { // an empty array or null), Dust uses the // original context before walking the path as // the context for rendering the else block - let new_breadcrumbs = Self::new_breadcrumbs( - breadcrumbs, + let new_breadcrumbs = Self::new_breadcrumbs_section( breadcrumbs, None, + Some(&injected_context), &container.explicit_context, None, ); @@ -259,12 +261,11 @@ impl<'a> DustRenderer<'a> { } } DustTag::DTExists(container) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, &container.explicit_context, - None, ); let val = walk_path(breadcrumbs, &container.path.keys); return if val.map(|v| v.is_truthy()).unwrap_or(false) { @@ -282,12 +283,11 @@ impl<'a> DustRenderer<'a> { }; } DustTag::DTNotExists(container) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, &container.explicit_context, - None, ); let val = walk_path(breadcrumbs, &container.path.keys); return if !val.map(|v| v.is_truthy()).unwrap_or(false) { @@ -307,12 +307,11 @@ impl<'a> DustRenderer<'a> { DustTag::DTPartial(partial) => { let partial_name = self.render_partial_name(&partial.name, breadcrumbs, blocks)?; if partial.params.is_empty() { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, &partial.explicit_context, - None, ); let rendered_content = self.render_template( &partial_name, @@ -322,12 +321,11 @@ impl<'a> DustRenderer<'a> { return Ok(rendered_content); } else { let injected_context = ParametersContext::new(breadcrumbs, &partial.params); - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, Some(&injected_context), &partial.explicit_context, - None, ); let rendered_content = self.render_template( &partial_name, @@ -342,12 +340,11 @@ impl<'a> DustRenderer<'a> { return Ok("".to_owned()); } DustTag::DTBlock(named_block) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, blocks.breadcrumbs, None, &named_block.explicit_context, - None, ); return match blocks.blocks.get_block(named_block.name) { None => self.render_maybe_body( @@ -363,12 +360,11 @@ impl<'a> DustRenderer<'a> { }; } DustTag::DTHelperEquals(parameterized_block) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, ¶meterized_block.explicit_context, - None, ); let param_map: HashMap<&str, &RValue<'a>> = Self::get_rval_map(¶meterized_block.params); @@ -414,12 +410,11 @@ impl<'a> DustRenderer<'a> { } } DustTag::DTHelperNotEquals(parameterized_block) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, ¶meterized_block.explicit_context, - None, ); let param_map: HashMap<&str, &RValue<'a>> = Self::get_rval_map(¶meterized_block.params); @@ -464,12 +459,11 @@ impl<'a> DustRenderer<'a> { } } DustTag::DTHelperGreaterThan(parameterized_block) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, ¶meterized_block.explicit_context, - None, ); let param_map: HashMap<&str, &RValue<'a>> = Self::get_rval_map(¶meterized_block.params); @@ -507,12 +501,11 @@ impl<'a> DustRenderer<'a> { } } DustTag::DTHelperGreaterThanOrEquals(parameterized_block) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, ¶meterized_block.explicit_context, - None, ); let param_map: HashMap<&str, &RValue<'a>> = Self::get_rval_map(¶meterized_block.params); @@ -550,12 +543,11 @@ impl<'a> DustRenderer<'a> { } } DustTag::DTHelperLessThan(parameterized_block) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, ¶meterized_block.explicit_context, - None, ); let param_map: HashMap<&str, &RValue<'a>> = Self::get_rval_map(¶meterized_block.params); @@ -593,12 +585,11 @@ impl<'a> DustRenderer<'a> { } } DustTag::DTHelperLessThanOrEquals(parameterized_block) => { - let new_breadcrumbs = Self::new_breadcrumbs( + let new_breadcrumbs = Self::new_breadcrumbs_partial( breadcrumbs, breadcrumbs, None, ¶meterized_block.explicit_context, - None, ); let param_map: HashMap<&str, &RValue<'a>> = Self::get_rval_map(¶meterized_block.params); @@ -744,7 +735,7 @@ impl<'a> DustRenderer<'a> { /// explicit_context /// new_context_element /// ``` - fn new_breadcrumbs<'b>( + fn new_breadcrumbs_deprecated<'b>( breadcrumbs: &'b Vec<&'b dyn ContextElement>, explicit_context_breadcrumbs: &'b Vec<&'b dyn ContextElement>, injected_context: Option<&'b dyn ContextElement>,