Skip to content

Commit 3eee0cc

Browse files
committed
Each, else not working fix #574
1 parent 3a60a48 commit 3eee0cc

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class EachHelper implements Helper<Object> {
4747
*/
4848
public static final String NAME = "each";
4949

50-
@SuppressWarnings({"rawtypes", "unchecked" })
50+
@SuppressWarnings({"rawtypes", "unchecked"})
5151
@Override
5252
public Object apply(final Object context, final Options options)
5353
throws IOException {
@@ -79,7 +79,7 @@ public Object apply(final Object context, final Options options)
7979
buffer.append(options.inverse());
8080
}
8181
return buffer;
82-
} else if (context != null) {
82+
} else if(context != null) {
8383
int index = 0;
8484
Iterator loop = options.propertySet(context).iterator();
8585
Context parent = options.context;
@@ -105,8 +105,9 @@ public Object apply(final Object context, final Options options)
105105
buffer.append(options.inverse());
106106
}
107107
return buffer;
108+
} else {
109+
return options.inverse();
108110
}
109-
return options.buffer();
110111
}
111112

112113
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.v4Test;
4+
import static org.junit.Assert.assertEquals;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
public class Issue574 extends v4Test {
10+
11+
@Test
12+
public void eachShouldExecuteElseBranchOnFalsyValue() throws Exception {
13+
shouldCompileTo("{{#each list}}not empty{{else}}empty{{/each}}",
14+
$("hash", $("list", Arrays.asList())), "empty");
15+
16+
shouldCompileTo("{{#each list}}not empty{{else}}empty{{/each}}",
17+
$("hash", $("list", null)), "empty");
18+
}
19+
}

0 commit comments

Comments
 (0)