back to Overview

_.format()

Formats an object using a template.
Util module only.

Syntax Variants

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

Parameters

template
The template as a string. The template, once created, will be cached.
object
the object to format
escapeFunction (optional)
The callback function(inputString) that will be used to escape all output:
inputString
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

Description

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().

Example

Format a name:

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

Example

Format a list of dates:

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

See also..

Comments

comments powered by Disqus

back to Overview

Functions