ractivejs

Determining the last loop index in ractive template


Basically I want to this: In Mustache templating is there an elegant way of expressing a comma separated list without the trailing comma? in a Ractive template.

For the object

{
  "items": [
    {"name": "red"},
    {"name": "green"},
    {"name": "blue"}
  ]
}

I want to produce "red, green, blue"
I want to know if I am at the last item, so I can know whether to print the separator. Something like:

{{#items:i}}{{name}} {{#i.is_last}},{{/i}}{{/items}}  

Solution

  • Can't easily test this right now, but wouldn't something like the following work?

    {{#items:i}}
        {{name}} {{ i < (items.length-1) ? "," : "" }}
    {{/items}}