StringTemplate: skipping trailing coma when generating a Javascript array

286 Views Asked by At

I'm generating a Javascript array in StringTemplate 4 and I'm having trouble skipping the trailing coma after last element. Each item gets generated using a template and then I want to separate them using comas to create an array in the form:

[ item1, item2, item3, item4 ]

Currently, my best shot at generating this array is this:

array(elems) ::= <<
  [ 
    $first(elems):elem_noComa()$
    $rest(elems):elem()$
  ]
>>

elem_noComa(el) ::= <<  { ...element generation... } >>

elem(el) ::= <<
  , $elem_noComa(el)$
>>

Is there a way to do it easier/in a shorter form?

1

There are 1 best solutions below

2
On BEST ANSWER

does this work for you?

<elems:itemTemplate(); separator=",">

It applies itemTemplate() to each element of elems array and using "," in between. Terence