]> git.proxmox.com Git - rustc.git/blobdiff - vendor/handlebars/src/helpers/helper_with.rs
New upstream version 1.47.0~beta.2+dfsg1
[rustc.git] / vendor / handlebars / src / helpers / helper_with.rs
index f329d52d09626618223426a2aaf212567ed635d9..4b68b783e22e6273f1f293489ab6a2bc12e09233 100644 (file)
@@ -1,4 +1,5 @@
-use crate::block::{BlockContext, BlockParams};
+use super::block_util::create_block;
+use crate::block::BlockParams;
 use crate::context::Context;
 use crate::error::RenderError;
 use crate::helpers::{HelperDef, HelperResult};
@@ -14,7 +15,7 @@ impl HelperDef for WithHelper {
     fn call<'reg: 'rc, 'rc>(
         &self,
         h: &Helper<'reg, 'rc>,
-        r: &'reg Registry,
+        r: &'reg Registry<'reg>,
         ctx: &'rc Context,
         rc: &mut RenderContext<'reg, 'rc>,
         out: &mut dyn Output,
@@ -27,16 +28,11 @@ impl HelperDef for WithHelper {
         let template = if not_empty { h.template() } else { h.inverse() };
 
         if not_empty {
-            let mut block = BlockContext::new();
-
-            let new_path = param.context_path();
-            if let Some(new_path) = new_path {
-                *block.base_path_mut() = new_path.clone();
-            }
+            let mut block = create_block(&param)?;
 
             if let Some(block_param) = h.block_param() {
                 let mut params = BlockParams::new();
-                if new_path.is_some() {
+                if param.context_path().is_some() {
                     params.add_path(block_param, Vec::with_capacity(0))?;
                 } else {
                     params.add_value(block_param, param.value().clone())?;
@@ -225,4 +221,29 @@ mod test {
         let r0 = handlebars.render("t0", &data);
         assert_eq!(r0.ok().unwrap(), "1".to_string());
     }
+
+    #[test]
+    fn test_else_context() {
+        let reg = Registry::new();
+        let template = "{{#with list}}A{{else}}{{foo}}{{/with}}";
+        let input = json!({"list": [], "foo": "bar"});
+        let rendered = reg.render_template(template, &input).unwrap();
+        assert_eq!("bar", rendered);
+    }
+
+    #[test]
+    fn test_derived_value() {
+        let hb = Registry::new();
+        let data = json!({"a": {"b": {"c": "d"}}});
+        let template = "{{#with (lookup a.b \"c\")}}{{this}}{{/with}}";
+        assert_eq!("d", hb.render_template(template, &data).unwrap());
+    }
+
+    #[test]
+    fn test_nested_derived_value() {
+        let hb = Registry::new();
+        let data = json!({"a": {"b": {"c": "d"}}});
+        let template = "{{#with (lookup a \"b\")}}{{#with this}}{{c}}{{/with}}{{/with}}";
+        assert_eq!("d", hb.render_template(template, &data).unwrap());
+    }
 }