Skip to content

Instantly share code, notes, and snippets.

@remcoder
Last active August 29, 2015 14:05
Show Gist options
  • Save remcoder/3bd66a8c33e0f981bf66 to your computer and use it in GitHub Desktop.
Save remcoder/3bd66a8c33e0f981bf66 to your computer and use it in GitHub Desktop.
Meteor / Blaze: register all of Underscore's methods as template helpers
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();
<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