Skip to content
thegrandpoobah edited this page Jun 20, 2012 · 3 revisions

API: Mustache.format

Description

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)

Signatures

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 ith argument of the function.

The argi parameter specifies the data object associated with the ith 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.

Examples

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'
]);
Clone this wiki locally