handlebars test
by jpeter06
HTML
<script src="https://cdn.jsdelivr.net/npm/handlebars@latest/dist/handlebars.js"></script>
JavaScript
Handlebars.registerHelper("total", function(a,b) {
var nums = [].slice.call(arguments,0,-1);
var sum = nums.reduce(function(prev,next) {
return prev + Number(next.replace(/[^0-9\.]+/g, ""));
},0);
return "$" + sum.toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
});
var template = Handlebars.compile("Handlebars <b>{{doesWhat}}</b><br>"+
"total: {{total 2 4}} <br>"+
"{{#eval \"_1 + _2\" as |z|}}");
// execute the compiled template and print the output to the console
document.body.innerHTML = template({ doesWhat: "rocks!" });