16

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 ?

1
  • Do you care about localization? (Using ',' or '.' as a separator depending on the locale) Nov 16, 2016 at 11:33

5 Answers 5

16

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}}
1
8

While I agree with Lucero's answer, it is possible to use a function in Mustache to format your data.

Simple template:

<ul>
    {{#price}}
        <li>{{commaFormat}}</li>
    {{/price}}
</ul>

JavaScript to process the data with a formatting function for your prices:

var tpl = $('#tpl').html(),
    data = {
        price: ['1234', '123', '123456', '1234567890'],
        commaFormat: function() {
            // adapted from https://stackoverflow.com/questions/2901102/how-to-print-number-with-commas-as-thousands-separators-in-javascript
            return this.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
        }
    },
    html = Mustache.to_html(tpl, data);

document.write(html);​

Resulting HTML:

<ul>
    <li>1,234</li>
    <li>123</li>
    <li>123,456</li>
    <li>1,234,567,890</li>
</ul>

Here's the working jsFiddle to inspect and play with further.

8

I have created a small extension for Mustache.js which enables the use of formatters inside of expressions, like {{expression | formatter}}

You can check it in github: http://jvitela.github.io/mustache-wax/

1
  • Thank you for this extension. It is working fine in Chrome and FF, but in IE 11 it is not working giving a JS error ( probably due to eval ). I m not sure Sep 17, 2019 at 17:38
1

You should do that before you pass your model into mustache (or any template engine really). Apart from being able to do string operationd, the issue is that not all countries format the numbers the same, so that the display representation may differ and shouldn't be hardcoded in the template.

1
  • 5
    some would argue that numbers are universal while the iso formatting is a cultural and a presentation issue Mar 12, 2014 at 21:58
-1

I use the Handlebars NumeralHelper package from https://www.npmjs.com/package/handlebars.numeral

First, install (and optionally save it to your package.json):

npm install --save handlebars.numeral

Next, load the helper:

var Handlebars = require('handlebars');
var NumeralHelper = require("handlebars.numeral");
NumeralHelper.registerHelpers(Handlebars);

Finally, use it in your page:

<script>
myNumber = 12300:
</script>

 <div>
 {{ number myNumber }}
 </div>

The output for this example is 12,300.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.