jQuery Plugin
jQuery.template()
Contents:
- jQuery.template( name , template )
- jQuery.template( name, template )
- jQuery.template( template )
- jQuery.template( template )
jQuery.template( name, template ) Returns: function
Description: Create a reusable named template (compiled from markup).
-
version added: 1.4.3jQuery.template( name, template )
nameA string naming the compiled template.
templateThe HTML markup and/or text to be used as template. Can be a string, or an HTML element (or jQuery object wrapping an element) whose content is to be used as template
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().
This method compiles the markup in the template
parameter as a named template, which can be referenced using the string specified in the name
parameter.
The return value is the compiled-template function.
See also template().
Note: The named template is added to the
$.template
map.
- To determine if a string
"someName"
is the name of a named template, test whether$.template["someName"]
is defined.- To remove a previously created named template, use
delete $.template["someName"];
Example: Create a compiled template associated with the name "summaryTemplate" and then reference it by name for rendering:
// Convert the markup string into a named template $.template( "summaryTemplate", "<li>${Name}</li>" ); function renderList() { // Render the movies data using the named template: "summaryTemplate" $.tmpl( "summaryTemplate", movies ).appendTo( "#moviesList" ); }
Example: Use the return value rather than the name
string to reference the compiled template:
// Convert the markup string into a compiled template var myTemplate = $.template( null, "<li>${Name}</li>" ); function renderList() { // Render movies data using the compiled template: myTemplate $.tmpl( myTemplate, movies ).appendTo( "#moviesList" ); }
Example: Create a named template and reference it by name as a nested template:
<script id="movieTemplate" type="text/x-jquery-tmpl"> {{tmpl "summaryTemplate"}} <tr><td>Director: ${Director}</td></tr> </script> ___________ // Convert the markup string into a named template, // referenced by the {{tmpl}} tag $.template( "summaryTemplate", "<tr><td>${Name}</td></tr>" ); // Render the movies data, using the named template as a nested template $( "#movieTemplate" ).tmpl( movies ).appendTo( "#movieList" );
Optimizing Template Rendering
When a template is rendered, using .tmpl() or jQuery.tmpl(), the markup is first converted into a compiled-template function. In the case of markup obtained from a string, the use of .template()
as in the above examples ensures that the conversion from markup to a compiled-template function only happens once.
On the other hand, passing a markup string template directly to .tmpl()
or to {{tmpl}}
for rendering will not be optimal from a performance point of view, since the markup will be re-compiled every time:
var markup = "<li>${Name}</li>"; function renderList() { // Sub-optimal: the markup string will be // recompiled each time renderList is called $.tmpl( markup, movies ).appendTo( "#moviesList" ); }
Note: In the case of inline templates declared within a script block, caching occurs automatically, so the following example does correspond to best practice:
Example: Rendering an inline template directly without compiling as a named template.
<script id="summaryTemplate" type="text/x-jquery-tmpl"> <li>${Name}</li> </script> ___________ function renderList() { // The template will be compiled only once, // so this is approach can be optimal. $( "#summaryTemplate" ).tmpl( movies ).appendTo( "#moviesList" ); }
Examples:
Example: Render template obtained from a markup string.
<!DOCTYPE html>
<html>
<head>
<style>
table { border-collapse:collapse; margin:8px; background-color:#f8f8f8; }
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>
<button id="showBtn">Show movies</button><br/>
<table><tbody id="movieList"></tbody></table>
<script>
var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
var markup = "<tr><td colspan='2'>${Name}</td><td>Released: ${ReleaseYear}</td><td>Director: ${Director}</td></tr>"
/* Compile markup string as a named template */
$.template( "movieTemplate", markup );
/* Render the named template */
$( "#showBtn" ).click( function() {
$( "#movieList" ).empty();
$.tmpl( "movieTemplate", movies ).appendTo( "#movieList" );
});
</script>
</body>
</html>
Example: Switch between templates obtained from markup strings.
<!DOCTYPE html>
<html>
<head>
<style>
table { border-collapse:collapse; margin:8px; background-color:#f8f8f8; }
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>
<button id="switchBtn">Show full details</button><br/>
<table><tbody id="movieList"></tbody></table>
<script>
var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
function renderTemplate( container, template, data ) {
$( container ).empty();
$.tmpl( template, data ).appendTo( container );
}
/* Compile markup as named templates */
$.template(
"titleTemplate",
"<tr><td>${Name}</td></tr>"
);
$.template(
"detailTemplate",
"<tr><td colspan='2'>${Name}</td><td>Released: ${ReleaseYear}</td><td>Director: ${Director}</td></tr>"
);
var details = false;
$( "#switchBtn" ).click( function() {
details = !details;
$(this).text( details ? "Show titles" : "Show full details" );
/* Render using the other named template */
renderTemplate( "#movieList", (details ? "detailTemplate" : "titleTemplate"), movies );
});
renderTemplate( "#movieList", "titleTemplate", movies );
</script>
</body>
</html>
Example: Use a markup string as a nested template.
<!DOCTYPE html>
<html>
<head>
<style>
table { border-collapse:collapse; border:2px solid blue; margin:8px; background-color:#f8f8f8; }
table tr { border:1px solid blue; } table td { padding:2px; }
.title { border-bottom:none; } .detail { border-top:none; }
</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">
{{tmpl "titleTemplate"}}
<tr class="detail"><td>Director: ${Director}</td></tr>
</tmpl>
<table><tbody id="movieList"></tbody></table>
<script>
var movies = [
{ Name: "The Red Violin", Director: "François Girard" },
{ Name: "Eyes Wide Shut", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", Director: "Mauro Bolognini" }
];
/* Convert the markup string into a named template,
referenced by the {{tmpl}} tag */
$.template( "titleTemplate", "<tr class='title'><td>${Name}</td></tr>" );
/* Render the movies data, using the named template as a nested template */
$( "#movieTemplate" ).tmpl( movies ).appendTo( "#movieList" );
</script>
</body>
</html>
jQuery.template( template ) Returns: function
Description: Returns a compiled-template function.
-
version added: 1.4.3jQuery.template( template )
templateThe template markup to be compiled, or a string corresponding to a named template.
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().
If the template
parameter is the name string for a named template created using $.template( name, template )
, this method returns the compiled template for the named template (equivalent to $.template[name]
).
Otherwise, if the template
parameter is a string containing HTML markup, then this method will return a compiled template for the markup provided.
If the template
parameter is a string containing pure text (no HTML tags), then the string is treated as a selector for an inline template, whose content will be used as markup. Similarly if template
is an HTML element (or jQuery object wrapping an element), then the content will be used as markup for the returned compiled template.
Example: Switch the template item to a different template, using $.template( name ), :
// Create the compiled templates $.template( "summaryTemplate", "<tr>...</tr>" ); $.template( "detailTemplate", "<tr>...</tr>" ); // Render the summaryTemplate with the "movies" data $.tmpl( "summaryTemplate", movies ).appendTo( "#movieList" ); $( "tr" ).click( function () { // Switch the template for this template item to // a different named template, then update the rendered item var tmplItem = $.tmplItem(this); tmplItem.tmpl = $.template( "detailTemplate" ); tmplItem.update(); });
Example:
Dynamic switching of templates, using $.template() to obtain compiled template.
<!DOCTYPE html>
<html>
<head>
<style>
table { cursor:pointer; border-collapse:collapse;
border:2px solid blue; width:300px; margin:8px; }
table tr { border:1px solid blue; color:blue; background-color:#f8f8f8; }
table td { padding:3px; } table tr:hover { color:red; }
.movieDetail { background-color:yellow; }
.movieDetail.row1 { border-bottom:none; }
.movieDetail.row2 { border-top:none; }
</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>
Click for details:
<table><tbody id="movieList"></tbody></table>
<script>
var movies = [
{ Name: "The Red Violin", ReleaseYear: "1998", Director: "François Girard" },
{ Name: "Eyes Wide Shut", ReleaseYear: "1999", Director: "Stanley Kubrick" },
{ Name: "The Inheritance", ReleaseYear: "1976", Director: "Mauro Bolognini" }
];
var selectedItem = null;
/* Create the compiled templates */
$.template(
"summaryTemplate",
"<tr class='movieSummary'><td colspan='2'>${Name}</td></tr>"
);
$.template(
"detailTemplate",
"<tr class='movieDetail row1'><td colspan='2'>${Name}</td></tr><tr class='movieDetail row2'><td>${ReleaseYear}</td><td>Director: ${Director}</td></tr>"
);
/* Render the summaryTemplate with the "movies" data */
$.tmpl( "summaryTemplate", movies ).appendTo( "#movieList" );
$( "#movieList" )
.delegate( ".movieSummary", "click", function () {
if (selectedItem) {
/* Switch the template for this template item to
the named template, then update the rendered item */
selectedItem.tmpl = $.template( "summaryTemplate" );
selectedItem.update();
}
selectedItem = $.tmplItem(this);
/* Switch the template for this template item */
selectedItem.tmpl = $.template( "detailTemplate" );
selectedItem.update();
})
.delegate( ".movieDetail", "click", function () {
/* Switch the template for this template item */
selectedItem.tmpl = $.template( "summaryTemplate" );
selectedItem.update();
selectedItem = null;
});
</script>
</body>
</html>