Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save usctrojan/3798990 to your computer and use it in GitHub Desktop.

Select an option

Save usctrojan/3798990 to your computer and use it in GitHub Desktop.
Handlebars.js helpers to iterate over objects
// Iterate through an object, passing each as 'key' and 'value'
//
// Usage:
// var t = Handlebars.compile('<ul>{{#key_value foo}}<li>{{key}}: {{value}}</li>{{/key_value}}</ul>');
// t({foo: {a:1, b:2}})
//
// Output:
// "<ul><li>a: 1</li><li>b: 2</li></ul>"
Handlebars.registerHelper("key_value", function(obj, fn) {
var buffer = "",
key;
for (key in obj) {
if (obj.hasOwnProperty(key)) {
buffer += fn({key: key, value: obj[key]});
}
}
return buffer;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment