Last active
October 5, 2015 17:21
-
-
Save zephster/942adc9cdfd9c6345a2d to your computer and use it in GitHub Desktop.
handlebars.js misc helpers
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
Handlebars.registerHelper({ | |
// usage: {{inc @index}} | |
inc: function(value) | |
{ | |
return parseInt(value) + 1; | |
}, | |
// usage: {{#if_eq a b}} or {{^if_eq a b}} for not equal | |
if_eq: function(a, b, c, options) | |
{ | |
if (typeof(c) === "object") | |
options = c; | |
if (a === b || a === c) | |
return options.fn(this); | |
else | |
return options.inverse(this); | |
}, | |
// usage: {{math "add" a b}}, {{math "sub" a b}} | |
math: function (op, a, b) | |
{ | |
var result; | |
try { | |
a = a.replace(',', ''); | |
b = b.replace(',', ''); | |
} catch (e) { | |
// intentionally do nothing | |
} | |
if (op === "add") | |
result = parseFloat(parseFloat(a) + parseFloat(b)).toFixed(2); | |
else if (op === "sub") | |
result = parseFloat(parseFloat(a) - parseFloat(b)).toFixed(2); | |
else if (op === "div") | |
result = parseFloat(parseFloat(a) / parseFloat(b)).toFixed(2); | |
return result; | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment