Handlebars {{math}} helper

by nethzor

HTML

<script src="//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.3.0/handlebars.js"></script>
<pre id="result"></pre>
{
}

JavaScript

Handlebars.registerHelper("math", function(lvalue, operator, rvalue, options) {
    lvalue = parseFloat(lvalue);
    rvalue = parseFloat(rvalue);
        
    return {
        "+": lvalue + rvalue,
        "-": lvalue - rvalue,
        "*": lvalue * rvalue,
        "/": lvalue / rvalue,
        "%": lvalue % rvalue
    }[operator];
});

var rendered = Handlebars.compile(
    '{{#each values}}' + 
    'i={{@index}}: ' +
    'i+1 = {{math @index "+" 1}}, ' + 
    'i-0.5 = {{math @index "+" "-0.5"}}, ' + 
    'i/2 = {{math @index "*" 2}}, ' + 
    'i%2 = {{math @index "%" 2}}, ' + 
    'i*i = {{math @index "*" @index}}\n' +
    '{{/each}}'
)({
    values : [ 'a', 'b', 'c', 'd', 'e' ]
});

$("#result").html(rendered);