Skip to content

Commit dea1fd4

Browse files
authored
Merge pull request #1172 from pwoodworth/issue-820
[ISSUE-820] Support helpers used without arguments in a block
2 parents 7803e86 + c52aa2e commit dea1fd4

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

  • handlebars/src

handlebars/src/main/java/com/github/jknack/handlebars/internal/Variable.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,6 @@ class Variable extends HelperResolver {
6767
/** Block params. */
6868
private static final List<String> BPARAMS = Collections.emptyList();
6969

70-
/** True, when no param/hash. */
71-
private boolean noArg;
72-
7370
/** Empty var. */
7471
private Template emptyVar;
7572

@@ -98,7 +95,6 @@ class Variable extends HelperResolver {
9895
this.escapingStrategy =
9996
type == TagType.VAR ? handlebars.getEscapingStrategy() : EscapingStrategy.NOOP;
10097
this.formatter = handlebars.getFormatter();
101-
this.noArg = params.size() == 0 && hash.size() == 0;
10298
postInit();
10399
}
104100

@@ -135,8 +131,9 @@ protected void merge(final Context scope, final Writer writer) throws IOExceptio
135131
*/
136132
@SuppressWarnings("unchecked")
137133
public Object value(final Context scope, final Writer writer) throws IOException {
138-
boolean blockParam = scope.isBlockParams() && noArg;
139-
if (helper != null && !blockParam) {
134+
boolean blockParam = scope.isBlockParams();
135+
Object value = scope.get(path);
136+
if (helper != null && (!blockParam || (path.size() == 1 && value == null))) {
140137
Options options =
141138
new Options(
142139
handlebars,
@@ -152,7 +149,6 @@ public Object value(final Context scope, final Writer writer) throws IOException
152149
options.data(Context.PARAM_SIZE, this.params.size());
153150
return helper.apply(determineContext(scope), options);
154151
} else {
155-
Object value = scope.get(path);
156152
if (value == null) {
157153
if (missing != null) {
158154
Options options =
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Handlebars.java: https://github.com/jknack/handlebars.java
3+
* Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0
4+
* Copyright (c) 2012 Edgar Espina
5+
*/
6+
package com.github.jknack.handlebars.i820;
7+
8+
import org.junit.jupiter.api.Test;
9+
10+
import com.github.jknack.handlebars.Handlebars;
11+
import com.github.jknack.handlebars.v4Test;
12+
13+
public class Issue820 extends v4Test {
14+
15+
@Override
16+
protected void configure(Handlebars handlebars) {
17+
handlebars.registerHelper("greeting", (context, options) -> "Hello");
18+
}
19+
20+
/** A helper that takes no arguments should still work within a block. */
21+
@Test
22+
public void helperWithoutArgumentsUsedInsideEachBlock() throws Exception {
23+
shouldCompileTo(
24+
"{{greeting}}\n{{#each users as |user|}}{{greeting}} {{user}}\n{{/each}}",
25+
$("hash", $("users", new Object[] {"Jack", "John"})),
26+
"Hello\nHello Jack\nHello John\n");
27+
}
28+
}

0 commit comments

Comments
 (0)