ArrayList<String> / mustache output

2,065 views
Skip to first unread message

Bobo

unread,
Jan 27, 2017, 11:40:15 AM1/27/17
to mustache.java
Hi,

I'm providing to mustache an Hashmap containg a key "myArray" associated to an ArrayList that contains only String values ["one","two","three"].

When I use such template {{myArray}}, java mustach output value is [one,two,three]. The double quotes are not anymore present... I'm expecting such result... ["one","two","three"].

Thank in advance for your help
Bobo

PS : I'm trying to create a template that output the element of this list with not trailing comma...

Sam Pullara

unread,
Jan 27, 2017, 12:36:13 PM1/27/17
to mustac...@googlegroups.com
When you use {{myArray}} it is just using the default toString() method on ArrayList. That method doesn't put quotes around the elements.

Sam


--
You received this message because you are subscribed to the Google Groups "mustache.java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mustachejava+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Aurélien Sanglard

unread,
Jan 27, 2017, 12:52:36 PM1/27/17
to mustac...@googlegroups.com
Thanks for your help.

Actually, I'm trying to generate such output ["one","two","three"]... but didn't yet find a solution.

Iterating on each element of the list with such template : '"{{.}}" ,' will lead to a trailing comma..
You received this message because you are subscribed to a topic in the Google Groups "mustache.java" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mustachejava/ugEeK1ANOJ0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mustachejava...@googlegroups.com.

Sam Pullara

unread,
Jan 27, 2017, 2:20:15 PM1/27/17
to mustac...@googlegroups.com
It is fairly ugly in mustache for this kind of thing as it works much better with HTML and the like where the last token isn't special. There is a class included with mustache.java called "DecoratedCollection" that gives you first/last/index that would help you in this case. Here is a test case that shows how it is used:


{{#myArray}}{{#first}}[{{/first}}{{^first}}, {{/first}}{{value}}{{#last}}]{{/last}}{{/myArray}}

I added a test that shows how it would work in your case.


Sam



To unsubscribe from this group and all its topics, send an email to mustachejava+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages