diff --git a/handlebars/src/main/java/com/github/jknack/handlebars/Context.java b/handlebars/src/main/java/com/github/jknack/handlebars/Context.java index 22e7f663..8e9890e3 100644 --- a/handlebars/src/main/java/com/github/jknack/handlebars/Context.java +++ b/handlebars/src/main/java/com/github/jknack/handlebars/Context.java @@ -21,6 +21,7 @@ import java.util.stream.Stream; import com.github.jknack.handlebars.context.MapValueResolver; +import com.github.jknack.handlebars.internal.path.PropertyPath; import com.github.jknack.handlebars.internal.path.ThisPath; import com.github.jknack.handlebars.io.TemplateSource; @@ -107,11 +108,21 @@ protected ParentFirst(final Object model) { @Override public Object get(final List path) { - Object value = parent.get(path); - if (value == null) { - return super.get(path); + // Only block params (lexically scoped) take precedence over the current model. Everything + // else resolves child first, like any other context. + PathExpression head = path.get(0); + if (head instanceof PropertyPath || (head instanceof ThisPath && path.size() > 1)) { + PathExpressionChain expr = new PathExpressionChain(path); + for (Context it = parent; it != null; it = it.parent) { + if (it instanceof BlockParam) { + Object value = expr.eval(resolver, it, it.model); + if (value != null && value != NULL) { + return value; + } + } + } } - return value; + return super.get(path); } @Override diff --git a/handlebars/src/test/java/com/github/jknack/handlebars/i1205/Issue1205.java b/handlebars/src/test/java/com/github/jknack/handlebars/i1205/Issue1205.java new file mode 100644 index 00000000..6bf9c96c --- /dev/null +++ b/handlebars/src/test/java/com/github/jknack/handlebars/i1205/Issue1205.java @@ -0,0 +1,49 @@ +/* + * Handlebars.java: https://github.com/jknack/handlebars.java + * Apache License Version 2.0 http://www.apache.org/licenses/LICENSE-2.0 + * Copyright (c) 2012 Edgar Espina + */ +package com.github.jknack.handlebars.i1205; + +import java.io.IOException; + +import org.junit.jupiter.api.Test; + +import com.github.jknack.handlebars.v4Test; + +public class Issue1205 extends v4Test { + + @Test + public void withInsideEachWithBlockParamsShouldResolveChildProperty() throws IOException { + shouldCompileTo( + "{{#each properties as |prop propName|}}{{#with prop.arrayItems}}" + + "{{typeName}}|{{this.typeName}}{{/with}}{{/each}}", + $( + "hash", + $( + "properties", + $("field", $("typeName", "ParentType", "arrayItems", $("typeName", "ChildType"))))), + "ChildType|ChildType"); + } + + @Test + public void blockParamsShouldStillBeVisibleInsideWith() throws IOException { + shouldCompileTo( + "{{#each properties as |prop propName|}}{{#with prop.arrayItems}}" + + "{{propName}}:{{prop.typeName}}:{{typeName}}{{/with}}{{/each}}", + $( + "hash", + $( + "properties", + $("field", $("typeName", "ParentType", "arrayItems", $("typeName", "ChildType"))))), + "field:ParentType:ChildType"); + } + + @Test + public void blockParamShouldWinOverChildProperty() throws IOException { + shouldCompileTo( + "{{#each items as |item|}}{{#with item.child}}{{item.child.item}}{{/with}}{{/each}}", + $("hash", $("items", $("a", $("child", $("item", "ChildItem"))))), + "ChildItem"); + } +}