Skip to content

with helper uses Parent-First resolution, causing parent fields to shadow child context fields #1167

@DatApplePy

Description

@DatApplePy

Description

When using the with helper, the context is correctly switched to the provided value. However, field resolution happens in a Parent-First manner. This means that if the parent context and the child context both contain a field with the same name, the parent field shadows the child one.

This behavior makes it look like with is not switching the context, even though it technically does.

Expected behavior

Inside a with block, property resolution should prefer the child context first. The parent context should only be considered if the property is not found in the child.

Minimal reproduction

Template:

{{typeName}} 
{{#with prop.arrayItems}}
  {{typeName}}
{{/with}}

Java model:

record CodegenType(String typeName) {}

var parent = Map.of(
  "typeName", "ParentType",
  "prop", Map.of("arrayItems", new CodegenType("ChildType"))
);

Template template = handlebars.compileInline(templateString);
System.out.println(template.apply(parent));

Actual output:

ParentType
ParentType

Expected output:

ParentType
ChildType

Environment

  • Java 21
  • Handlebars.java 4.5.0

Question

Is this Parent-First resolution inside with blocks an intentional design choice? In Mustache, variable lookup typically walks from the current (child) context outward toward parents (i.e., Child-First). Should with in handlebars.java be aligned with that behavior, or is the current resolution order by design? If intended, would you consider documenting this explicitly (or offering an option to prefer Child-First within with)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions