Last active
August 29, 2015 14:05
-
-
Save remcoder/3bd66a8c33e0f981bf66 to your computer and use it in GitHub Desktop.
Meteor / Blaze: register all of Underscore's methods as template helpers
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
function registerUnderscoreHelpers() { | |
for (key in _) { | |
var prop = _[key]; | |
if (typeof(prop) == 'function' ) { | |
createUnderscoreHelper(key, prop); | |
} | |
} | |
} | |
// Spacebars always passes an extra argument to the helper, an | |
// object of type Spacebars.kw. | |
// To make underscore function work with optional parameters | |
// that last argument is stripped from the argument list. | |
function createUnderscoreHelper(key, fun) { | |
UI.registerHelper('_'+key, function() { | |
var args = Array.prototype.slice.call(arguments, 0, arguments.length-1); | |
return fun.apply(null, args); | |
}); | |
} | |
registerUnderscoreHelpers(); |
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
<ul> | |
{{#each _range 42}} | |
<li>number {{.}}</li> | |
{{/each}} | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment