Skip to content

Commit e2056e4

Browse files
committed
Dynamic Partial (lookup) with a default parameter doesn't work fix #531
1 parent 65f746f commit e2056e4

4 files changed

Lines changed: 37 additions & 6 deletions

File tree

handlebars/src/main/antlr4/com/github/jknack/handlebars/internal/HbsParser.g4

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ partialBlock
170170

171171
pexpr
172172
:
173-
LP sexpr RP hash* #dynamicPath
174-
| path = (QID|PATH) QID? hash* #staticPath
173+
LP sexpr RP QID? hash* #dynamicPath
174+
| path = (QID|PATH) QID? hash* #staticPath
175175
| path = (DOUBLE_STRING | SINGLE_STRING) QID? hash* #literalPath
176176
;
177177

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public Object apply(final Object context, final Options options)
5959
Context ctx = Context.newBuilder(options.context, context).build();
6060
Object lookup = ctx.get(options.param(0).toString());
6161
if (lookup == null) {
62-
return null;
62+
return context;
6363
}
6464
return lookup;
6565
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ public Object visitSubParamExpr(final SubParamExprContext ctx) {
482482
SexprContext sexpr = ctx.sexpr();
483483
return new VarParam(
484484
newVar(sexpr.QID().getSymbol(), TagType.SUB_EXPRESSION, params(sexpr.param()),
485-
hash(sexpr.hash()), ctx.start.getText(), ctx.stop.getText(), false));
485+
hash(sexpr.hash()), "(", ")", false));
486486
}
487487

488488
@Override
@@ -638,12 +638,13 @@ public PartialInfo visitDynamicPath(final DynamicPathContext ctx) {
638638
SexprContext sexpr = ctx.sexpr();
639639
TerminalNode qid = sexpr.QID();
640640
Template expression = newVar(qid.getSymbol(), TagType.SUB_EXPRESSION, params(sexpr.param()),
641-
hash(sexpr.hash()), ctx.start.getText(), ctx.stop.getText(), false);
641+
hash(sexpr.hash()), "(", ")", false);
642642

643643
PartialInfo partial = new PartialInfo();
644644
partial.path = expression;
645645
partial.hash = hash(ctx.hash());
646-
partial.context = null;
646+
TerminalNode scope = ctx.QID();
647+
partial.context = scope != null ? scope.getText() : null;
647648
partial.token = qid.getSymbol();
648649
return partial;
649650
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
import java.util.Arrays;
7+
8+
import org.junit.Test;
9+
10+
import com.github.jknack.handlebars.v4Test;
11+
12+
public class Hbs531 extends v4Test {
13+
14+
@Test
15+
public void dynamicPartialLookupWithADefaultParameterDoesn_tWork() throws IOException {
16+
shouldCompileTo("{{#each this}} {{> (lookup profession 'name') this.person}}<br /> {{/each}}",
17+
$("hash", Arrays.asList(
18+
$("firstName", "John", "lastName", "Foe", "profession", "developer"),
19+
$("firstName", "James", "lastName", "Doe", "profession", "analyst")),
20+
"partials",
21+
$("analyst", "Analyst - {{firstName}} {{lastName}}",
22+
"developer", "Developer - {{firstName}} {{lastName}}")),
23+
" Developer - John Foe<br /> Analyst - James Doe<br /> ");
24+
25+
assertEquals("{{#each this}} {{>(lookup profession 'name') this.person}}<br /> {{/each}}",
26+
compile("{{#each this}} {{> (lookup profession 'name') this.person}}<br /> {{/each}}")
27+
.text());
28+
}
29+
30+
}

0 commit comments

Comments
 (0)