back to Overview


Formats an object using a template.
Util module only.

Syntax Variants

_.format(template, object)
_.format(template, object, escapeFunction)


The template as a string. The template, once created, will be cached.
the object to format
escapeFunction (optional)
The callback function(inputString) that will be used to escape all output:
The string to escape.
(callback return value)
The escaped string.
If no escapeFunction has been given, the output will not be escaped. _.escapeHtml() can be used as an escape function for HTML, and _.escapeRegExp() for regular expressions. JavaScript's built-in escape() function can escape URL components. See _.htmlFormat() for a version of format() that already includes HTML escaping.
(return value)
the string created by the template


Formats an object using a template. The template syntax is shared with _.template(). The only difference is that format() frees you from the extra step of creating the template. In any case, whether you use format() or _.template(), the template will be cached. Be careful when you create templates dynamically, as every template is cached and consumes memory.
If you only want to format a single value, use _.formatValue().


Format a name:

var s = _.formatHtml("{{first}} {{last}}", {first: 'Tim', last: 'Taylor'});


Format a list of dates:

var s = _.format("{{each}}{{this :: yyyy-MM-dd}}{{/each}}", dateList);

See also..


comments powered by Disqus

back to Overview