Skip to content

Instantly share code, notes, and snippets.

@daleyjem
Created December 1, 2015 18:58
Show Gist options
  • Save daleyjem/6e3f6d1f2059be9472d5 to your computer and use it in GitHub Desktop.
Save daleyjem/6e3f6d1f2059be9472d5 to your computer and use it in GitHub Desktop.
Handlebars Eval Helper
/**
* Evalutes a given expression by taking in a hash of variables as input values.
* ${variable} denotes a variable assigned in the hash.
*
* Example:
* {{eval "${a} + ${b}" a=1 b=2}}
* = 3
*/
module.exports.register = function(Handlebars, options) {
Handlebars.registerHelper('eval', function(expr, options) {
var reg = new RegExp("\\${(\\S+)}", "g");
var compiled = expr.replace(reg, function(match, pull){
return options.hash[pull];
});
var evaluated = eval(compiled);
return evaluated;
});
}
@ppbntl19
Copy link

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