-
Notifications
You must be signed in to change notification settings - Fork 2
API format
format
provides a means of mimicking a limited subset of the functionality of the printf
/String.format
family of functions by performing positional argument interpolations on a template string. The format
function is basically syntactic sugar for the following invocation of the render function:
Mustache.render(template, array)
Mustache.format(template, [arg1, arg2, arg3, ..., argn])
The template
parameter specifies the template to interpolate the arguments with. The template is a limited form of the general Mustache templates in that the tag keys SHOULD be a natural number. All other tag keys will be ignored. Each tag corresponds to the i
th argument of the function.
The arg
i parameter specifies the data object associated with the i
th tag key in the template
parameter. The relationship is not checked. That is, if more parameters than keys are supplied, the extra parameters are ignored. Similarly, if insufficient parameters are provided, the extra keys will be interpolated as the empty string.
Simple interpolation:
var result = Mustache.format('{{0}} is awesome.', 'Mustache');
result === 'Mustache is awesome.'; // true
Keys do not have to be ordered:
var result = Mustache.format('{{1}} is {{0}}.', 'awesome', 'Mustache');
result === 'Mustache is awesome.'; // true
The parameters can be of any type that Mustache understands:
var result = Mustache.format('{{#0}}{{.}}{{/0}}', [
'Bugs Bunny',
'Daffy Duck',
'Wile E. Coyote'
]);