Mustache.js lambdas and toFixed number formatting

I am trying to customize a mustache.js template that formats a number to a specific decimal place using lambda and I am encountering problems. Given an object that looks like this:

{
     x: 123,
     points: [
          { name: "foo", y: 1.234567 },
          { name: "bar", y: 2.123456 },
          { name: "fax", y: 3.623415 }
     ]
}

At first I tried to create a template that would look like this:

var template = "{{x}}{{#points}}<br/>{{name}}, {{#y.toFixed}}2{{/y.toFixed}}";

This did not work (generated an empty space where the number should be). Although, perhaps, the lambda was not in the correct format, since toFixed does not return a function ( documents for mustaches ). So I tried:

Number.prototype.toMustacheFixed = function(){
     var n = this;
     return function(d){ return n.toFixed(d); };
};
var template = "{{x}}{{#points}}<br/>{{name}}, {{#y.toMustacheFixed}}2{{/y.toMustacheFixed}}"

Again unsuccessfully. I even tried to simplify the toMustacheFixed function for:

Number.prototype.toMustacheFixed = function(){
     return function(){ return 123.45; };
};

It did not help. I was still getting an empty template. So, can Mustache.js just not handle native and prototype functions on numbers, or am I doing something wrong?

+5
1

: http://jsfiddle.net/QXFY4/10/

: {{/points}}

toFixed, Lambdas, http://mustache.github.com/mustache.5.html

{{y}} toFixed .

+10

All Articles