Last active
December 15, 2015 11:39
-
-
Save marlun78/5255001 to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* Handlebars eachIn helper | |
* Copyright (c) 2013, marlun78 | |
* MIT License, https://gist.github.com/marlun78/bd0800cf5e8053ba9f83 | |
* | |
* Description: | |
* A Handlebars helper to iterate over object properties rather than arrays. | |
* | |
* Usage: | |
* <script id="list-object" type="text/x-handlebars-template"> | |
* <table> | |
* <caption>The object contains:</caption> | |
* <tr><th>Key</th><th>Value</th></tr> | |
* {{#eachIn data}} | |
* <tr><td>{{key}}</td><td>{{value}}</td></tr> | |
* {{/eachIn}} | |
* </table> | |
* </script> | |
* <script> | |
* //Handlebars.registerHelper('eachIn', ... | |
* | |
* jQuery(function($){ | |
* 'use strict'; | |
* var tmpl = $('#list-object').html(), | |
* generateObjectList = Handlebars.compile(tmpl), | |
* html = generateObjectList({ data: {'a': 1, 'b': 2, 'c': 3 } }); | |
* $('body').append(html); | |
* }); | |
* </script> | |
*/ | |
Handlebars.registerHelper('eachIn', function (context, options) { | |
var key, | |
fn = options.fn, | |
ret = ''; | |
for (key in context) { | |
if (context.hasOwnProperty(key)) { | |
ret += fn({ key: key, value: context[key] }); | |
} | |
} | |
return ret; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment