Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions handlebars/src/main/java/com/github/jknack/handlebars/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -107,11 +108,21 @@ protected ParentFirst(final Object model) {

@Override
public Object get(final List<PathExpression> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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");
}
}
Loading