Skip to content

Instantly share code, notes, and snippets.

@zephster
Last active October 5, 2015 17:21
Show Gist options
  • Save zephster/942adc9cdfd9c6345a2d to your computer and use it in GitHub Desktop.
Save zephster/942adc9cdfd9c6345a2d to your computer and use it in GitHub Desktop.
handlebars.js misc helpers
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