jknack / handlebars.java

Logic-less and semantic Mustache templates with Java
http://jknack.github.io/handlebars.java
Other
1.48k stars 383 forks source link

Support for iterating Maps with non-string keys #502

Closed CWThomson closed 8 years ago

CWThomson commented 8 years ago

It appears that when trying to iterate over a Map with non-string keys (e.g. Map<Object,Object>) we get an ClassCastException

java.lang.ClassCastException: com.xyz.ObjectGroup cannot be cast to java.lang.String inline@c686e86:94:11 at com.github.jknack.handlebars.helper.EachHelper.apply(EachHelper.java:89) at com.github.jknack.handlebars.internal.Block.merge(Block.java:211) at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:128) at com.github.jknack.handlebars.internal.TemplateList.merge(TemplateList.java:94) at com.github.jknack.handlebars.internal.BaseTemplate.apply(BaseTemplate.java:128)

The following code demonstrates the problem:

    Map<Object,Collection<String>> data = new TreeMap<>();
    Collection<String> d1 = new ArrayList<>();
    d1.add("1");
    d1.add("2");
    d1.add("3");

    Collection<String> d2 = new ArrayList<>();
    d2.add("4");
    d2.add("5");
    d2.add("6");

    data.put(123, d1);
    data.put(456, d2);

    Map<String,Object> params = new HashMap<>();
    params.put("data", data);

    String result = new Handlebars().compileInline( "{{#each data}}{{ @key }} - {{#each . }}Val:{{.}}{{/each}}{{/each}}").apply(params);`

Is there a reason this is not supported as the code changes required to support it look trivial

jknack commented 8 years ago

Fixed.

Invite you to try http://jooby.org

Thanks