Skip to content

Instantly share code, notes, and snippets.

@palanik
Created April 16, 2016 20:06
Show Gist options
  • Save palanik/25ef563812ecbdabe3d2e70bc8dc92b5 to your computer and use it in GitHub Desktop.
Save palanik/25ef563812ecbdabe3d2e70bc8dc92b5 to your computer and use it in GitHub Desktop.
Handlebar.js generic lodash helper
Handlebars.registerHelper("_", function() {
var options = [].pop.call(arguments);
var func = [].shift.call(arguments);
return _[func].apply(_, arguments);
});
// Usage:
{{_ 'functionName' arg1 arg2 argn}}
// Example - _.round() → 4.01
{{_ 'round' 4.006, 2}}
@JimiHFord
Copy link

What is

var options = [].pop.call(arguments);

that line doing? It appears options is unused 🤔

@ghostrydr
Copy link

@JimiHFord

Handlebars adds an options object as the last parameter when it calls the handler. This line cherry picks that object off so that it's no longer part of arguments when it's passed to the lodash method. Same thing with this line:

var func = [].shift.call(arguments);

It removes the first parameter so that all that is passed to the lodash method is the data/object that you want to operate on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment