Skip to content

Commit 849fd5c

Browse files
giflwjknack
authored andcommitted
Add missing keys on context of EachHelper (#543)
Add @key to Iterable and @index to Map as handlebars.js (https://github.com/wycats/handlebars.js/blob/master/lib/handlebars/helpers/each.js) (2016-09-27)
1 parent 1f6c48e commit 849fd5c

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

  • handlebars/src/main/java/com/github/jknack/handlebars/helper

handlebars/src/main/java/com/github/jknack/handlebars/helper/EachHelper.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ public Object apply(final Object context, final Options options)
6262
while (loop.hasNext()) {
6363
Object it = loop.next();
6464
Context itCtx = Context.newContext(parent, it);
65-
itCtx.combine("@index", index)
65+
itCtx.combine("@key", index)
66+
.combine("@index", index)
6667
.combine("@first", index == base ? "first" : "")
6768
.combine("@last", !loop.hasNext() ? "last" : "")
6869
.combine("@odd", even ? "" : "odd")
@@ -79,6 +80,7 @@ public Object apply(final Object context, final Options options)
7980
}
8081
return buffer;
8182
} else if (context != null) {
83+
int index = 0;
8284
Iterator loop = options.propertySet(context).iterator();
8385
Context parent = options.context;
8486
boolean first = true;
@@ -90,11 +92,13 @@ public Object apply(final Object context, final Options options)
9092
Object value = entry.getValue();
9193
Context itCtx = Context.newBuilder(parent, value)
9294
.combine("@key", key)
95+
.combine("@index", index)
9396
.combine("@first", first ? "first" : "")
9497
.combine("@last", !loop.hasNext() ? "last" : "")
9598
.build();
9699
buffer.append(options.apply(fn, itCtx, Arrays.asList(value, key)));
97100
first = false;
101+
index++;
98102
}
99103
// empty?
100104
if (first) {

0 commit comments

Comments
 (0)