0

Actually I'm using "com.github.spullara.mustache.java" but that should not be important. I have array of strings. If I try to render it using just:

{
    "codes": {{variable}}
}

then it get rendered

{
    "codes": [a, b]
}

which is close to OK. But we are missing apostrophes.

Is there a way how to add those? I tried this:

[{{#codes}}"{{.}}"{{/codes}}]

which is even closer, but misses comma in the middle (darn!)

{
    "codes": ["a""b"]
}

and if I add the comma:

[{{#codes}}"{{.}}",{{/codes}}]

there will be (no surprise) extra at the end.

Is there a way how to do that? Changing the input data structure is not an option for me.

Martin Mucha
  • 1,226
  • 12
  • 24

1 Answers1

0

So far I can only come up with a nasty hack of trimming the final html.

// after rendering.
var eleHTML = $("#target60").html();
eleHTML = eleHTML.substring(0, eleHTML.length - 6);
$("#target60").html(eleHTML);

Play around with how much to lop off the end. If your data is always the same length you might get away with this.


Edit:

Looks like this was covered quite a bit afterall. Looks like there's nothing built-in for this:

In Mustache templating is there an elegant way of expressing a comma separated list without the trailing comma?

wazz
  • 4,057
  • 5
  • 18
  • 32
  • well fiddling with input (like having special variable for N-1 records and another for last record of array) or fiddling with "rendered" output is exactly the stuff we want to avoid, and we're trying to do so via templating language. If that is not possile, it's very strong argument for ditching mustache altogether. Thanks for answer, but I'm hoping that there's better way than this. – Martin Mucha Sep 04 '19 at 19:25
  • I'm new to this templating language. I checked some more and it seems, that handlebars, which are extension to mustache does have conditionals and have access to array length. Therefore if I'm not mistaken, this must be possible to implement there. I'll try that, as I can freely move to handlebars and I'm not locked into mustache. – Martin Mucha Sep 04 '19 at 20:38
  • 1
    Cool. It was mentioned briefly in the link I added, in case you didn't see my update. – wazz Sep 04 '19 at 20:41
  • I should be more careful when reading provided links from others. It could save me significant amount of time. Thank you very much sir for your help!! – Martin Mucha Sep 05 '19 at 05:34