jQuery API

jQuery Plugin

{{html}} Template Tag

{{html fieldNameOrExpression}}

Description: Used for insertion of HTML markup strings in the rendered template. Evaluates the specified field on the current data item, or the specified JavaScript function or expression.

  • version added: 1.4.3{{html fieldNameOrExpression}}

    fieldNameOrExpressionThe name of a field on the current data item, or a JavaScript function or expression, returning HTML markup.

This documentation topic concerns the jQuery Templates plugin (jquery-tmpl), which can be downloaded from: http://github.com/jquery/jquery-tmpl.

Note: For information about how to render templates, see .tmpl() and jQuery.tmpl().

Template Tags

Template tags such as the {{html}} tag can be used within jQuery templates in addition to text and HTML markup, in order to enable a number of scenarios such as composition of templates, iteration over hierarchical data, parameterization of template rendering, etc.

Other available tags include: ${}, {{each}}, {{if}}, {{else}}, {{tmpl}} and {{wrap}}. User-defined template tags can also be specified, by extending the jQuery.tmpl.tag map.

Using the {{html}} Template Tag

The following example shows how to use {{html}} to insert markup from the Synopsis field of the data item into the rendered template.

<script id="movieTemplate" type="text/x-jquery-tmpl">
    <h4>${Name}</h4>
    <p>{{html Synopsis}}</p>
</script>

HTML encoding

Using {{html fieldNameOrExpression}} is equivalent to using ${fieldNameOrExpression}, except that it renders unencoded text into the HTML DOM, whereas ${} encodes values by default.

Evaluating Expressions and Functions, Using Template Variables

{{html expression}} can be used in a similar way to ${expression}, to render markup returned by an expression or a function call, as in the following example:

Template:
<p>{{html $item.getSynopsis(true)}</p>
Code:
// Render the template with the movie data
$( "#movieTemplate" ).tmpl( movie, { 
    getSynopsis: function( short ) {
        //return short or long synopsis
        //...
    }
}).appendTo( "#movieContainer" );

See ${} for more detailed documentation and examples of using template tags in association with expression evaluation, function calls, template variables, etc.

Example:

Using {{html}} to insert markup from data.

<!DOCTYPE html>
<html>
<head>
  <style>
.role {font-weight:bold;font-style: italic;} #movieContainer {padding-left: 8px;}
</style>
  <script src="http://code.jquery.com/jquery-latest.min.js"></script>
  <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script>
</head>
<body>
  
<tmpl id="movieTemplate" type="text/x-jquery-tmpl"> 
    <h4>${Name}</h4>
    <p>{{html Synopsis}}</p>
</tmpl>

<div id="movieContainer"></div>

<script>
/* The Synopsis data field contains HTML markup. */
var movie = { 
    Name: "Meet Joe Black", 
    Synopsis: "The <span class='role'>grim reaper</span> (<a href='http://www.netflix.com/RoleDisplay/Brad_Pitt/73919'>Brad Pitt</a>) visits <span class='role'>Bill Parrish</span> (<a href='http://www.netflix.com/RoleDisplay/Anthony_Hopkins/43014'>Anthony Hopkins</a>)..."
};

/* Render the template with the movie data.
   The template uses the {{html}} template tag
   to  insert the Synopsis HTML markup data. */
$( "#movieTemplate" ).tmpl( movie )
    .appendTo( "#movieContainer" );
</script>

</body>
</html>

Demo:

Support and Contributions

Need help with {{html}} Template Tag or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to {{html}} Template Tag? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • http://twitter.com/rapextras rapextras

    very very very very cool… taking .json requests and parsing them for template layout/html on the page, will save a lot of the pre-, post- tagging and necc. back-end prep work – outta the equation :)

  • Whobutsb

    Is there any way to set the default so that all ${ tagName } will be rendered as HTML instead of adding {{html tagName}?

    • http://www.borismoore.com Boris Moore

      ${} is equivalent to {{= }}, and is a different tag than {{html}}. Use the one which corresponds to your need. The more abbreviated syntax was chosen to be the one that encodes, since for security reasons it is better to encode by default. Choose {{html}} if you are sure you don't want encoding.

  • http://www.borismoore.com Boris Moore

    ${} is equivalent to {{= }}, and is a different tag than {{html}}. Use the one which corresponds to your need. The more abbreviated syntax was chosen to be the one that encodes, since for security reasons it is better to encode by default. Choose {{html}} if you are sure you don't want encoding.

  • Grebe

    Unfortunately {{html thing}} filters out newlines. It would be useful to have a function that allowed complete transparency i.e everything gets through. The situation I have is users entering text in a textarea including newlines and spacing. I wanted to render this via a template into a DIV with CSS { white-space: pre; } to preserve the users whitespace. My get around is to $(“#id”).text(thing) after the template has been fulfilled.