2

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?

machinery
  • 3,793
  • 4
  • 41
  • 52

1 Answers1

6

does this work for you?

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

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

Terence Parr
  • 5,912
  • 26
  • 32
  • 1
    Thank you very much Professor Parr. Silly of me. I was expecting this to work differently and must have not even checked this correctly. The above works. Marking as answer. Nice to meet you by the way :) Greetings from the royal city of Kraków. – machinery May 25 '12 at 05:19
  • 1
    Glad to help! Greetings from San Francisco. – Terence Parr May 26 '12 at 19:35