Created
July 27, 2016 13:57
-
-
Save danrichards/e4276b818b0d8deafd799836230d9dce to your computer and use it in GitHub Desktop.
Comparison Helper for Handlebars JS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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