Skip to content

Instantly share code, notes, and snippets.

@scttnlsn
Created May 14, 2012 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scttnlsn/2697405 to your computer and use it in GitHub Desktop.
Save scttnlsn/2697405 to your computer and use it in GitHub Desktop.
Handlebars object iteration
Handlebars.registerHelper('eachobj', function(object, options) {
var buffer = '';
var empty = true;
for (var key in object) {
buffer += options.fn({ key: key, value: object[key] });
empty = false;
}
if (empty) {
return options.inverse(this);
} else {
return buffer;
}
});
{{#eachobj data}}
{{ this.key }} - {{ this.value }}
{{else}}
Object is empty!
{{/eachobj}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment