javascript - underscore.js templating, iterating over "this" -
how iterate on "this" (or data object passed template) using underscore.js templating? example, if pass data object template:
obj = {name: "ben", description: "funny looking"} var template = _.template("<ul> <% _.each(this, function(x, y){ print('<li>'+x+'<li>')}) %> </ul>"); var compiled = template(obj); i expect "this" in iterator loop on data object (like in handlebars), it's not working. ordinarily i'd directly enter <%= name %> template, in case, object keys being determined dynamically.
any ideas on how this? thanks!
here's easy solution:
var compiled = template({data:obj}); and iterate on "data"
Comments
Post a Comment