Skip to content

Instantly share code, notes, and snippets.

@marcofugaro
Created November 16, 2015 19:31
Show Gist options
  • Save marcofugaro/790fc6821c5124e3d74b to your computer and use it in GitHub Desktop.
Save marcofugaro/790fc6821c5124e3d74b to your computer and use it in GitHub Desktop.
If operator in Handlebars.js
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
switch (operator) {
case '==':
return (v1 == v2) ? options.fn(this) : options.inverse(this);
case '===':
return (v1 === v2) ? options.fn(this) : options.inverse(this);
case '!=':
return (v1 != v2) ? options.fn(this) : options.inverse(this);
case '!==':
return (v1 !== v2) ? options.fn(this) : options.inverse(this);
case '<':
return (v1 < v2) ? options.fn(this) : options.inverse(this);
case '<=':
return (v1 <= v2) ? options.fn(this) : options.inverse(this);
case '>':
return (v1 > v2) ? options.fn(this) : options.inverse(this);
case '>=':
return (v1 >= v2) ? options.fn(this) : options.inverse(this);
case '&&':
return (v1 && v2) ? options.fn(this) : options.inverse(this);
case '||':
return (v1 || v2) ? options.fn(this) : options.inverse(this);
default:
return options.inverse(this);
}
});
//usage
{{#ifCond var1 '==' var2}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment