Skip to content

Instantly share code, notes, and snippets.

@asselin
Created May 21, 2012 15:32
Show Gist options
  • Select an option

  • Save asselin/2762936 to your computer and use it in GitHub Desktop.

Select an option

Save asselin/2762936 to your computer and use it in GitHub Desktop.
Handlebars helper for generic Javascript expressions
This helper allows you to use generic javascript expressions in a handlebars template. It can be used to call Javascript functions on a value or do data structure access that Handlebars doesn't support (e.g. accessing an element of an array).
<input type="text" data-dojo-type="dijit.form.TextBox" value='{{jsexpr "val[1]"}}'/>
Handlebars.registerHelper('jsexpr', function(expr) {
if (arguments.length < 2)
throw new Error("Handlerbars Helper 'arrayaccess' needs 1 parameter");
with (this) {return eval(expr)};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment