samskivert / jmustache

A Java implementation of the Mustache templating language.
Other
834 stars 128 forks source link

"-index" is 0 when used inside conditional block within list block #90

Closed Maia-Everett closed 6 years ago

Maia-Everett commented 7 years ago

I have a class like this:

public final class Foo {
    private final String name;
    private final Integer quantity;

    public Foo(final String name, final Integer quantity) {
        this.name = name;
        this.quantity = quantity;
    }

    public String getName() { return name; }
    public Integer getQuantity() { return quantity; }
}

and I have a template block like this:

{{#fooList}}
{{#quantity}}|q{{-index}}={{quantity}}{{/quantity}}|{{name}}
{{/fooList}}

Basically, I want blocks like |q1=42 displayed only when quantity is not null. Unfortunately, {{-index}} is only set to the correct element index if used directly inside the {{#fooList}} list block; it is 0 when used inside the condition block, so the expression evaluates to something like |q0=42.

I think that the behavior of {{-index}} should be changed to refer to the index corresponding to the innermost list block. It should only be 0 if the statement is being evaluated outside of any list block whatsoever.

samskivert commented 6 years ago

Sorry for the five month delay! Busy summer.