jQuery & jQuery UI Documentation

jQuery & jQuery UI

jQuery Plugin

{{wrap}} Template Tag

{{wrap( [data], [options] ) template}} content {{/wrap}}

Description: Used for composition of templates which incorporate wrapped HTML content. Rendered template items can combine wrapped HTML content with template markup.

  • version added: 1.4.3{{wrap( [data], [options] ) template}} content {{/wrap}}

    templateThe HTML markup or text to use as a template.

    dataThe data to render. This can be any JavaScript type, including Array or Object.

    optionsAn optional map of user-defined key-value pairs. Extends the tmplItem data structure, available to the template during rendering.

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 {{wrap}} 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}}, {{html}} and {{tmpl}}. User-defined template tags can also be specified, by extending the jQuery.tmpl.tag map.

The {{wrap}} and {{tmpl}} Template Tags

The {{wrap}} template tag is similar to the {{tmpl}} template tag, except that it provides additional support for incorporating wrapped HTML content into the rendered output. The use of the template, data and options parameters is the same for both tags. (See {{tmpl}} for additional information and examples).

When using {{wrap}} the template parameter will usually correspond to template markup which uses the $item.html() feature to incorporate the wrapped markup. The template parameter can be any of the following:

  • A string containing markup.
  • An HTML element (or jQuery object that wraps an element) whose content is to be used as the template.
  • A string corresponding to the name of a named template (see jQuery.template() and .template()).
  • A compiled-template function (see jQuery.template() and .template()).

With {{wrap}}, the data parameter will often be unspecified, since the rendered output can be driven by the wrapped HTML rather than by data. If data is specified and is an array, the template is rendered once for each data item in the array. If data is an object, or if the data parameter is missing or null, a single template item is rendered.

Using the {{wrap}} Template Tag

The following example shows how to use {{wrap}} to render a nested template that incorporates the wrapped HTML. In this case the content of the {{wrap}} template tag is a set of div elements. The chosen template, tableWrapper, extracts each div and wraps it in a table cell.

<script id="myTmpl" type="text/x-jquery-tmpl">
    The following wraps some HTML content:
    {{wrap "#tableWrapper"}}
        <div>
            First <b>content</b>
        </div>
        <div>
            And <em>more</em> <b>content</b>...
        </div>
    {{/wrap}}
</script>

<script id="tableWrapper" type="text/x-jquery-tmpl">
    <table><tbody>
        <tr>
            {{each $item.html("div")}}
                <td>
                    {{html $value}}
                </td>
            {{/each}}
        </tr>
    </tbody></table>
</script>

The $item.html() Method

Any HTML content between the opening and closing tags of {{wrap}} is made available to the template as a $item.html( filter, textOnly ) method on the template item.

The return value of $item.html() is a filtered array of markup strings taken from the wrapped content:

  • Use $item.html() without parameters to return all the top-level elements of the wrapped HTML content.
  • Pass a selector string as filter parameter to return a filtered subset of the top-level elements. (The default value of filter is "*").
  • If textOnly is set to true $item.html( filter, textOnly ) returns the inner text of the filtered subset of top-level elements.

The following example shows how to incorporate both HTML and text content into the rendered output.

<script id="myTmpl" type="text/x-jquery-tmpl">
    The following wraps and reorders some HTML content:
    {{wrap "#tableWrapper"}}
        <h3>One</h3>
        <div>
            First <b>content</b>
        </div>
        <h3>Two</h3>
        <div>
            And <em>more</em> <b>content</b>...
        </div>
    {{/wrap}}
</script>

<script id="tableWrapper" type="text/x-jquery-tmpl">
    <table><tbody>
        <tr>
            {{each $item.html("h3", true)}}
                <td>
                    ${$value}
                </td>
            {{/each}}
        </tr>
        <tr>
            {{each $item.html("div")}}
                <td>
                    {{html $value}}
                </td>
            {{/each}}
        </tr>
    </tbody></table>
</script>

Including Template Tags in Wrapped Content

The HTML markup between the opening and closing tags of {{wrap}} can include other template tags. The following example uses nested {{wrap}}tags to created nested tab views:

<script id="myTmpl" type="text/x-jquery-tmpl">
    {{wrap "#tabsWrap"}}
        <h3>One</h3>
        <div>
            First <b>tab</b>
        </div>
        <h3>Two</h3>
        <div>
            <b>Second</b> tab
            {{wrap "#tabsWrap"}}
                <h3>InnerOne</h3>
                <div>
                    Inner first <b>tab</b>
                </div>
                <h3>InnerTwo</h3>
                <div>
                    Inner <b>second</b> tab
                </div>
            {{/wrap}}
        </div>
    {{/wrap}}
</script>

Examples:

Example: Using {{wrap}} to wrap elements in table cells.

<!DOCTYPE html>
<html>
<head>
  <style>
table { border-collapse:collapse; width:380px; background-color:#f8f8f8; border:2px solid blue; margin:5px; } table td { border:1px solid blue; padding:3px; } 
</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="myTmpl" type="text/x-jquery-tmpl">
    The following wraps and reorders some HTML content:
    {{wrap "#tableWrapper"}}
        <h3>One</h3>
        <div>
            First <b>content</b>
        </div>
        <h3>Two</h3>
        <div>
            And <em>more</em> <b>content</b>...
        </div>
    {{/wrap}}

    And this wraps different HTML content:
    {{wrap "#tableWrapper"}}
        <div>
            First <b>div</b>
        </div>
        <div>
            Second <b>div</b>
        </div>
        <div>
            Third <b>div</b>
        </div>
        <h3>first h3</h3>
        <h3>second h3</h3>
        <h3>third h3</h3>
    {{/wrap}}
</tmpl>

<tmpl id="tableWrapper" type="text/x-jquery-tmpl">
    <table><tbody>
        <tr>
            {{each $item.html("h3", true)}}
                <td>
                    ${$value}
                </td>
            {{/each}}
        </tr>
        <tr>
            {{each $item.html("div")}}
                <td>
                    {{html $value}}
                </td>
            {{/each}}
        </tr>
    </tbody></table>
</tmpl>

<div id="myWrappedView"></div>

<script>
$( "#myTmpl" ).tmpl()
    .appendTo( "#myWrappedView" );
</script>

</body>
</html>

Example: Using {{wrap}} to create a tabbed view.

<!DOCTYPE html>
<html>
<head>
  <style>
.body {height:80px;background-color:#fff;} .body div {height:32px;vertical-align:middle;text-align:center;} .body h3 {text-align:center;} .tabsView td {border:solid 1px #0000A6;border-top:none;border-right:solid 2px #1E1ED2;}
.tabsView th {cursor:pointer;padding:2px;font-weight:normal;font-style:italic;color:#888;border:solid 1px #bbb;border-right:none;background-color:#f8f8f8;border-bottom:solid 1px #1E1ED2;} #tabsView > .tabsView {width:265px;} 
.tabsView {width:250px;border-collapse:collapse;border:none;margin:5px;} .tabsView tr {border-right:solid 1px #bbb;} th.header_true {font-weight:bold;border:solid 1px #0000A6;border-right:solid 2px #1E1ED2;border-bottom:solid 1px #eee;color:#0000A6;background-color:#fff;} 
</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="tabsTmpl" type="text/x-jquery-tmpl">
    {{wrap(null, {state: $item.state}) "#tabsWrap"}}
        <h3>One</h3>
        <div>
            First <b>tab</b>
        </div>
        <h3>Two</h3>
        <div>
            <b>Second</b> tab
        </div>
        <h3>Three</h3>
        <div>
            Third <em>tab</em> <br/> and more content...
        </div>
    {{/wrap}}
</tmpl>

<tmpl id="tabsWrap" type="text/x-jquery-tmpl">
    <table class="tabsView"><tbody>
        <tr>
            {{each $item.html("h3", true)}}
                <th class="header_${$index === $item.state.activeIndex}">
                    ${$value}
                </th>
            {{/each}}
        </tr>
        <tr><td colspan='${$item.html("h3").length}'>
            <div class="body">
                {{html $item.html("div")[$item.state.activeIndex]}}
            </div>
        </td></tr>
    </tbody></table>
</tmpl>

<div id="tabsView"></div>

<script>
/* Track the selected tab index */
var state = { activeIndex: 1 };

/* Render tabs view */
$( "#tabsTmpl" ).tmpl( null, { state: state})
    .appendTo( "#tabsView" );

$( "#tabsView" )
    .delegate( ".tabsView th", "click", function() {
        var tmplItem = $.tmplItem( this );

        /* Set the selected tab index to this tab */
        tmplItem.state.activeIndex = $(this).index();

        /* update the rendering */
        tmplItem.update();
    });
</script>

</body>
</html>

Example: Using {{wrap}} to create nested tabbed views.

<!DOCTYPE html>
<html>
<head>
  <style>
.body {height:85px;background-color:#fff;} .body div {height:30px;vertical-align:middle;text-align:center;} .body h3 {text-align:center;} .tabsView td {border:solid 1px #0000A6;border-top:none;border-right:solid 2px #1E1ED2;}
.tabsView th {cursor:pointer;padding:2px;font-weight:normal;font-style:italic;color:#888;border:solid 1px #bbb;border-right:none;background-color:#f8f8f8;border-bottom:solid 1px #1E1ED2;} #tabsView > .tabsView {width:265px;} 
.tabsView {width:250px;border-collapse:collapse;border:none;margin:5px;} .tabsView tr {border-right:solid 1px #bbb;} th.header_true {font-weight:bold;border:solid 1px #0000A6;border-right:solid 2px #1E1ED2;border-bottom:solid 1px #eee;color:#0000A6;background-color:#fff;} 
</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="tabsTmpl" type="text/x-jquery-tmpl">
    {{wrap(null, {state: $item.state}) "#tabsWrap"}}
        <h3>One</h3>
        <div>
            First <b>tab</b>
        </div>
        <h3>Two</h3>
        <div>
            <b>Second</b> tab
            {{wrap(null, {state: $item.state.innerState}) "#tabsWrap"}}
                <h3>InnerOne</h3>
                <div>
                    Inner first <b>tab</b>
                </div>
                <h3>InnerTwo</h3>
                <div>
                    Inner <b>second</b> tab
                </div>
            {{/wrap}}
        </div>
        <h3>Three</h3>
        <div>
        Third <em>tab</em> <br/> and more content...
        </div>
    {{/wrap}}
</tmpl>

<tmpl id="tabsWrap" type="text/x-jquery-tmpl">
    <table class="tabsView"><tbody>
        <tr>
            {{each $item.html("h3", true)}}
                <th class="header_${$index === $item.state.activeIndex}">
                    ${$value}
                </th>
            {{/each}}
        </tr>
        <tr><td colspan='${$item.html("h3").length}'>
            <div class="body">
                {{html $item.html("div")[$item.state.activeIndex]}}
            </div>
        </td></tr>
    </tbody></table>
</tmpl>

<div id="tabsView">..loading</div>

<script>
/* Track the selected tab index for inner and outer tab views */
var state = { activeIndex: 1, innerState: { activeIndex: 0 } };

function refresh() {
    $( "#tabsView" ).empty();
    $( "#tabsTmpl" ).tmpl( null, { state: state})
        .appendTo( "#tabsView" );
}

/* Render tabs view */
refresh();

$( "#tabsView" )
    .delegate( ".tabsView th", "click", function() {
        /* Set the selected tab index to this tab */
        $.tmplItem( this ).state.activeIndex = $(this).index();

        /* update the rendering */
        refresh();
    });
</script>

</body>
</html>