Skip to content

Instantly share code, notes, and snippets.

@danrichards
Created July 27, 2016 13:57
Show Gist options
  • Save danrichards/e4276b818b0d8deafd799836230d9dce to your computer and use it in GitHub Desktop.
Save danrichards/e4276b818b0d8deafd799836230d9dce to your computer and use it in GitHub Desktop.
Comparison Helper for Handlebars JS.
// @see http://bdadam.com/blog/comparison-helper-for-handlebars.html
(function() {
function checkCondition(v1, operator, v2) {
switch(operator) {
case '==':
return (v1 == v2);
case '===':
return (v1 === v2);
case '!==':
return (v1 !== v2);
case '<':
return (v1 < v2);
case '<=':
return (v1 <= v2);
case '>':
return (v1 > v2);
case '>=':
return (v1 >= v2);
case '&&':
return (v1 && v2);
case '||':
return (v1 || v2);
default:
return false;
}
}
Handlebars.registerHelper('ifCond', function (v1, operator, v2, options) {
return checkCondition(v1, operator, v2)
? options.fn(this)
: options.inverse(this);
});
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment