Skip to content

Commit

Permalink
Adding support for 1-based index by introducing the @index_1 psuedo v…
Browse files Browse the repository at this point in the history
…ariable
  • Loading branch information
Eli Williams committed Jan 22, 2015
1 parent 9c5ea54 commit 99887ac
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ private CharSequence iterableContext(final Iterable<Object> context, final Optio
.combine("@last", last ? "last" : "")
.combine("@odd", even ? "" : "odd")
.combine("@even", even ? "even" : "")
// 1-based index
.combine("@index_1", index + 1)
.build();
buffer.append(options.fn(current));
current.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,27 @@ public class PseudoVarsTest {
@Test
public void list() throws IOException {
String input =
"{{#list}}i={{@index}}\neven={{@even}}\nodd={{@odd}}\nfirst={{@first}}\nlast={{@last}}\n{{/list}}";
"{{#list}}i={{@index}}\neven={{@even}}\nodd={{@odd}}\nfirst={{@first}}\nlast={{@last}}\ni+1={{@index_1}}\n{{/list}}";
Handlebars handlebars = new Handlebars();

assertEquals("i=0\n" +
"even=even\n" +
"odd=\n" +
"first=first\n" +
"last=\n" +
"i+1=1\n" +
"i=1\n" +
"even=\n" +
"odd=odd\n" +
"first=\n" +
"last=\n" +
"i+1=2\n" +
"i=2\n" +
"even=even\n" +
"odd=\n" +
"first=\n" +
"last=last\n",
"last=last\n" +
"i+1=3\n",
handlebars.compileInline(input).apply(new Object() {
@SuppressWarnings("unused")
public List<String> getList() {
Expand Down

0 comments on commit 99887ac

Please sign in to comment.