Skip to content

subscript syntax should be a superset of the dot syntax #366

@bohnman

Description

@bohnman

The subscript syntax somevar[someprop] should do all of the things that the dot syntax somevar.someprop does plus be able to index arrays and pass in variables.

Here are a couple of scenarios where they differ

Scenario 1: referencing a property of an undefined variable.

public static void main(String[] args) {
    { // works fine
        JtwigModel model = JtwigModel.newModel();
        JtwigTemplate template = JtwigTemplate.inlineTemplate("{{foo.bar}}");
        template.render(model);
    }

    { // throws CalculationException

        JtwigModel model = JtwigModel.newModel();
        JtwigTemplate template = JtwigTemplate.inlineTemplate("{{foo['bar']}}");
        template.render(model);
    }

}

Scenario 2: Referencing a bean property

public static class Foo {
    private String bar = "baz";

    public String getBar() {
        return bar;
    }

    public void setBar(String bar) {
        this.bar = bar;
    }
}

public static void main(String[] args) {
    { // works fine
        JtwigModel model = JtwigModel.newModel();
        model.with("foo", new Foo());
        JtwigTemplate template = JtwigTemplate.inlineTemplate("{{foo.bar}}");
        template.render(model);
    }

    { // throws CalculationException

        JtwigModel model = JtwigModel.newModel();
        model.with("foo", new Foo());
        JtwigTemplate template = JtwigTemplate.inlineTemplate("{{foo['bar']}}");
        template.render(model);
    }

}

This would make it more inline with similar templating languages

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions