which javascript template engine is nest suited to generate JSON

Both mustache and handlebars are awesome. I like them both for their individual simplicity and excellence. Mustache because it's a one template works in lots of places and handlebars because it provides just a few more features.

The challenge I have is that they seem to have been implemented to output HTML or some other document structure where the tags are in pairs and there are no separators.

To further clarify, if you have an array of items that would be output in a list then this works well:

<ul>
   {{#each items}}
   <li>{{name}}</li>
   {{/each}}
</ul>

This works great. But if you want to output something JSON-like:

[
   {{#each items}}
       { name:{{name}} }
   {{/each}}
]

This does not work because JSON requires that there be commas separating items in a list. And you cannot put a comma after the innermost '}' because that would cause an error too.

There are several posts/recommendations where people have requested that an optional separator attribute be added to the #each or adding a #join. One committer said that it should be implemented as a plug-in because the core needs to be simple.

The politics aside. Being able to format a javascript object as a JSON string seems well suited for templates.

**One final idea. There may actually be a better Javascript Idiom for reformating a JavaScript object. I suppose that would also be interesting to consider.

PS: the one reason I like the template is because it becomes self documenting.

UPDATE:

@Kevin over at the handlebarsjs team was able to create a "Helper" function that implemented the feature I was missing. It's not like to make it into the core any time soon but that code worked:

[
   {{#join items sep=','}}
       { name:{{name}} }
   {{/join}}
]