Skip to content

Commit f551148

Browse files
authored
feat: add @index for object iteration (#682)
1 parent a970c28 commit f551148

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/helpers/helper_each.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ impl HelperDef for EachHelper {
130130
block.set_local_var("first", to_json(is_first));
131131
block.set_local_var("last", to_json(is_last));
132132
block.set_local_var("key", key.clone());
133+
block.set_local_var("index", to_json(i));
133134

134135
update_block_context(block, obj_path, k.to_string(), is_first, v);
135136
set_block_param(block, h, obj_path, &key, v)?;
@@ -190,7 +191,7 @@ mod test {
190191
assert!(handlebars
191192
.register_template_string(
192193
"t1",
193-
"{{#each this}}{{@first}}|{{@last}}|{{@key}}:{{this}}|{{/each}}",
194+
"{{#each this}}{{@first}}|{{@last}}|{{@key}}:{{this}}|{{@index}}|{{/each}}",
194195
)
195196
.is_ok());
196197

@@ -207,7 +208,7 @@ mod test {
207208
let r1 = handlebars.render("t1", &m);
208209
assert_eq!(
209210
r1.ok().unwrap(),
210-
"true|false|ftp:21|false|false|gopher:70|false|true|http:80|".to_string()
211+
"true|false|ftp:21|0|false|false|gopher:70|1|false|true|http:80|2|".to_string()
211212
);
212213
}
213214

0 commit comments

Comments
 (0)