back to Overview

_.formatHtml()

Formats an object using a template with HTML escaping for the output.
Util module only.

Syntax Variants

_.formatHtml()
_.formatHtml(template, object)

Parameters

template
The template as a string. The template, once created, will be cached.
object
the object to format
(return value)
the string created by the template

Description

Formats an object using a template with HTML escaping for the output. The template syntax is shared with _.template(). Output in double curly braces is automatically escaped using _.escapeHtml(). formatHtml() just creates a new template with HTML escaping and invokes it immediately. 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 = _.formatHtml("{{each}}{{::yyyy-MM-dd}}{{/each}}", dateList);

See also..

  • ht() works uses formatHtml to set element's innerHTML.
  • HTML() create HTML nodes using formatHtml.
  • _.template() creates a template function, using the same syntax.
  • _.format() allows you to specify alternative escape mechanisms.

Comments

comments powered by Disqus

back to Overview

Functions