how can i format decimal to currency format in mustache?

brainydexter picture brainydexter · Jul 18, 2012 · Viewed 21.9k times · Source

I'm using mustache to render json data received through Ajax.

I want to render this number in currency format:

{{price}}

e.g.: 12300

How can I render it as:

"12,300"

using mustache templating engine ?

Answer

Otto Allmendinger picture Otto Allmendinger · Jul 18, 2012

Mustache is very minimalistic and doesn't provide any support for this.

You either have to supply the formatted data or a function returning the formatted data

{ priceDecimal: function () { return formatDecimal(this.price); } }

Another alternative is to use handlebars.js which supports helper functions

Handlebars.registerHelper('decimal', function(number) {
  return formatDecimal(number);
});

and use it like this

{{decimal price}}