Forked from strathmeyer/handlebars.object_helpers.js
Created
September 28, 2012 10:06
-
-
Save usctrojan/3798990 to your computer and use it in GitHub Desktop.
Handlebars.js helpers to iterate over objects
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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