How do I iterate over “this” (or the data object that is passed to the template) using underscore.js templating? For example, if I pass a data object like this into a 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 would expect “this” in my iterator to loop over the data object (like in Handlebars), but it’s not working. Ordinarily I’d directly enter <%= name %> into the template, but in this case, the object keys are being determined dynamically.
Any ideas on how to do this? Thanks!
Here’s an easy solution:
And then just iterate over “data”