node.js - which javascript template engine is nest suited to generate JSON -
both mustache , handlebars awesome. them both individual simplicity , excellence. mustache because it's 1 template works in lots of places , handlebars because provides few more features.
the challenge have seem have been implemented output html or other document structure tags in pairs , there no separators.
to further clarify, if have array of items output in list works well:
<ul> {{#each items}} <li>{{name}}</li> {{/each}} </ul>
this works great. if want output json-like:
[ {{#each items}} { name:{{name}} } {{/each}} ]
this not work because json requires there commas separating items in list. , cannot put comma after innermost '}' because cause error too.
there several posts/recommendations people have requested optional separator attribute added #each or adding #join. 1 committer said should implemented plug-in because core needs simple.
the politics aside. being able format javascript object json string seems suited templates.
**one final idea. there may better javascript idiom reformating javascript object. suppose interesting consider.
ps: 1 reason template because becomes self documenting.
update:
@kevin on @ handlebarsjs team able create "helper" function implemented feature missing. it's not make core time code worked:
[ {{#join items sep=','}} { name:{{name}} } {{/join}} ]
Comments
Post a Comment