Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save asselin/2762904 to your computer and use it in GitHub Desktop.
Handlebars block helper to extend if to support generic Javascript logical expressions
{{#jsif "astring=='abc'"}}
Is true
{{else}}
Is false
{{/jsif}}
This function adds support to Handlebars for evaluating generic javascript expressions (i.e. what if SHOULD have been).
Handlebars.registerHelper('jsif', function(expr, options) {
if (arguments.length < 2)
throw new Error("Handlerbars Helper 'jsif' needs a parameter");
with (this) {var result = eval(expr)};
if (result) {
return options.fn(this);
} else {
return options.inverse(this);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment