Repository: sunng87/handlebars-rust
Pull request #276 (2019-07-07 20:04:52)

Support singular and plural block params for each helper
With a single block param, `each` should set it as the value of the container being iterated over.

With two block param, `each` should set the first as the value of the container being iterated over, and the second as the key/index.


Patch:
diff --git a/.travis.yml b/.travis.yml
index f46bb2c..d8e7954 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -9,7 +9,7 @@ matrix:
     - rust: nightly
     - rust: beta
     - rust: stable
-    - rust: 1.32.0
+    - rust: 1.31.0
     - rust: nightly
       name: minimal-versions
       script:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f794dd8..fbd1f10 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,18 +1,10 @@
 # Change Log
 
-## [{{2.0.0}}](https://github.com/sunng87/handlebars-rust/compare/2.0.0-beta3...{{2.0.0}}) - {{ 2019-07-02 }}
-* [Changed] Fixed more dyn trait warnings
-* [Changed] #80 Fixed support for zero-param helper
-* [Changed] Changed minimum Rust version to 1.32 as required by
-  getrandom crate
-
 ## [2.0.0-beta.3](https://github.com/sunng87/handlebars-rust/compare/2.0.0-beta1...2.0.0-beta.3) - {{ 2019-06-24}}
 
 * [Changed] Block parameter revamp, fixed cases for #260 and #264
 * [Changed] #265 Fixed block parameter order in `each` helper
 * [Changed] #266 Accept any JSON value in boolean helpers
-* [Changed] `RenderContext` API update, `evaluate_absolute` removed,
-  use `@root` instead
 
 ## [2.0.0-beta.1](https://github.com/sunng87/handlebars-rust/compare/1.1.0...2.0.0-beta.1) - {{ 2019-03-16}}
 
diff --git a/Cargo.toml b/Cargo.toml
index 7f6d913..dddd927 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,7 +1,7 @@
 [package]
 
 name = "handlebars"
-version = "2.0.1-alpha.0"
+version = "2.0.0-beta.3"
 authors = ["Ning Sun <sunng@pm.me>"]
 description = "Handlebars templating implemented in Rust."
 license = "MIT"
diff --git a/README.md b/README.md
index ce2f81e..60ef09e 100644
--- a/README.md
+++ b/README.md
@@ -67,7 +67,6 @@ and clarify in CHANGELOG.
 
 | Handlebars version range | Minimum Rust version |
 | --- | --- |
-| ~2.0.0 | 1.32 |
 | ~1.1.0 | 1.30 |
 | ~1.0.0 | 1.23 |
 
diff --git a/src/helpers/helper_each.rs b/src/helpers/helper_each.rs
index 188598a..5664168 100644
--- a/src/helpers/helper_each.rs
+++ b/src/helpers/helper_each.rs
@@ -56,16 +56,22 @@ impl HelperDef for EachHelper {
                                 local_rc.set_path(new_path);
                             }
 
-                            if let Some(block_param) = h.block_param() {
+                            if let Some(bp_val) = h.block_param() {
                                 let mut params = BlockParams::new();
-                                params.add_path(block_param, local_rc.get_path())?;
+                                params.add_path(bp_val, local_rc.get_path())?;
+
+                                local_rc.push_block_context(params)?;
+                            } else if let Some((bp_val, bp_index)) = h.block_param_pair() {
+                                let mut params = BlockParams::new();
+                                params.add_path(bp_val, local_rc.get_path())?;
+                                params.add_value(bp_index, to_json(i))?;
 
                                 local_rc.push_block_context(params)?;
                             }
 
                             t.render(r, ctx, &mut local_rc, out)?;
 
-                            if h.block_param().is_some() {
+                            if h.has_block_param() {
                                 local_rc.pop_block_context();
                             }
 
@@ -97,7 +103,12 @@ impl HelperDef for EachHelper {
                                 local_rc.set_path(new_path);
                             }
 
-                            if let Some((bp_val, bp_key)) = h.block_param_pair() {
+                            if let Some(bp_val) = h.block_param() {
+                                let mut params = BlockParams::new();
+                                params.add_path(bp_val, local_rc.get_path())?;
+
+                                local_rc.push_block_context(params)?;
+                            } else if let Some((bp_val, bp_key)) = h.block_param_pair() {
                                 let mut params = BlockParams::new();
                                 params.add_path(bp_val, local_rc.get_path())?;
                                 params.add_value(bp_key, to_json(&k))?;
@@ -107,7 +118,7 @@ impl HelperDef for EachHelper {
 
                             t.render(r, ctx, &mut local_rc, out)?;
 
-                            if h.block_param().is_some() {
+                            if h.has_block_param() {
                                 local_rc.pop_block_context();
                             }
 
diff --git a/src/lib.rs b/src/lib.rs
index 54bf2f7..3449923 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,4 @@
-#![doc(html_root_url = "https://docs.rs/handlebars/2.0.0")]
+#![doc(html_root_url = "https://docs.rs/handlebars/2.0.0-beta.3")]
 //! # Handlebars
 //!
 //! [Handlebars](http://handlebarsjs.com/) is a modern and extensible templating solution originally created in the JavaScript world. It's used by many popular frameworks like [Ember.js](http://emberjs.com) and Chaplin. It's also ported to some other platforms such as [Java](https://github.com/jknack/handlebars.java).
diff --git a/src/render.rs b/src/render.rs
index 0337a39..2b38ed7 100644
--- a/src/render.rs
+++ b/src/render.rs
@@ -394,6 +394,11 @@ impl<'reg: 'rc, 'rc> Helper<'reg, 'rc> {
         self.block
     }
 
+    /// Returns if the helper has either a block param or block param pair
+    pub fn has_block_param(&self) -> bool {
+        self.block_param.is_some()
+    }
+
     /// Returns block param if any
     pub fn block_param(&self) -> Option<&str> {
         if let Some(&BlockParam::Single(Parameter::Name(ref s))) = self.block_param {


Test patch:
diff --git a/tests/block_context.rs b/tests/block_context.rs
index c52d303..dae4b71 100644
--- a/tests/block_context.rs
+++ b/tests/block_context.rs
@@ -32,3 +32,17 @@ fn test_root_with_blocks() {
         "{{#*inline \"test\"}}{{b}}:{{@root.b}};{{/inline}}{{#each a}}{{> test}}{{/each}}";
     assert_eq!(hbs.render_template(template, &data).unwrap(), "1:3;2:3;");
 }
+
+#[test]
+fn test_singular_and_pair_block_params() {
+    let hbs = Handlebars::new();
+
+    let data = json!([
+        {"value": 11},
+        {"value": 22},
+    ]);
+
+    let template =
+        "{{#each this as |b index|}}{{b.value}}{{#each this as |value key|}}:{{key}},{{/each}}{{/each}}";
+    assert_eq!(hbs.render_template(template, &data).unwrap(), "11:value,22:value,");
+}