jQuery Templates – tmpl(), template() and tmplItem()

In the first blog, I made introduction to the jQuery Templates plugin made by Microsoft for the jQuery library. Now in this blog I will pass briefly through the three main functions that are supported by the plugin up to now.

- tmpl() and $.tmpl()

The purpose of this function is to render the template

Syntax:
tmpl([data], [options])
$.tmpl(template, [data], [options])

As you can see, the first tmpl() function takes two parameters, while the second takes three parameters. What is the real difference here?

Both functions make the same. The difference is that in the second $.tmpl (with jQuery sign) we have template parameter so that this parameter can be string containing markup, HTML Element, or Name of named template. To see this in real-world example, see the complete example at the bottom of this blog.

tmpl() Example

$("#myTemplate").tmpl(myData).appendTo("ul");


$.tmpl() Example

$.tmpl("namedTemplate", myData).appendTo("ul");

 

- template() and $.template()

We can use this function to create named templates. Previously I’ve written an example with $.tmpl() where it’s first parameter is named template. We can create named templates using tempalte or $.template() and use it anywhere else. So, with this function we can simply compile the template by associating it with the given name.

Syntax:
template([name])
$.template(name, template)


Let’s see two simple examples:

template() Example

 

<script id="myTemplate" type="text/html">
    <li> ${Attendee} </li>
</script>
$("#myTemplate").template("attendeeTemplate");


$.template()

$.template("attendeeTemplate", "<li>${Attendee}</li>");

Usage of the above example would be

$.tmpl("attendeeTemplate", Data).appendTo("ol")

now you see both $.tmpl and $.template in same scenario

 

- tmplItem() and $.tmplItem()

Using this function, we can access the rendered template item. It will retrieve object which has data and nodes properties.

Syntax:
tmplItem()
$.tmplItem(element)

And here are the examples:
tmplItem()

var item = $("font").tmplItem();

$.tmplItem()

var item = $.tmplItem("font");

 

Here is complete example where I’m using all three functions in one scenario

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>jQuery Templates – tmpl(), template() and tmplItem()</title>
    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">
        $(function () {
            var attendees = [
                { Name: "Hajan", Surname: "Selmani", speaker: true, phones: [070555555, 071888999, 071222333] },
                { Name: "Darko", Surname: "Milevski", phones: [070555555, 071888999, 071222333] },
                { Name: "Ljubomir", Surname: "Zivanovic", phones: [070555555, 071222333] },
                { Name: "Mile", Surname: "Grujovski", phones: [070555555, 071888999, 071222333] },
                { Name: "Ivan", Surname: "Acev", phones: [071888999, 071222333] },
                { Name: "Dejan", Surname: "Dimitrovski", speaker: true, phones: [070555555, 071222333] }
                ];

            $("#attendeesTemplate").template("listAttendees"); //compiling the template to named listAttendees

            $.tmpl("listAttendees", attendees).appendTo("#attendees"); //using compiled template
            //$("#attendeesTemplate").tmpl(attendees).appendTo("#attendees");

            $("#findSpeaker").click(function () {
                var speakers = $("li.speaker:last").tmplItem();
                var speaker = speakers.data;
                var htmlElement = speakers.nodes;
                $(htmlElement).css("background-color", "yellow");
            });

            $("#addNew").click(function () {
                var sps = false;
                if ($("#speaks").attr("checked")) sps = true;

                attendees.push({ Name: $("#name").val(), Surname: $("#surname").val(), speaker: sps });
                $("#attendees").html("");
                $.tmpl("listAttendees", attendees).appendTo("#attendees");
            });
        });
    </script>

    <script id="attendeesTemplate" type="text/html">
            {{if speaker}}
                <li class="speaker">${Name} ${Surname}
                (<font color="green">speaker</font>)
                </li>
            {{else}}
                <li class="attendee">${Name} ${Surname}
                    (attendee)
                </li>
            {{/if}}        
    </script>
</head>
<body>
<div id="content">
    <div id="list" style="width:300px;">
        <ol id="attendees"></ol>
    </div>
    <div id="addForm">
        Name: <input id="name" type="text" style="display:inline;" />  
        Surname: <input id="surname" type="text" />
        Speaker: <input id="speaks" type="checkbox" /> <br />
        <a id="addNew" href="#">Add New</a><br />
    </div>            
    <a id="findSpeaker" href="#">HighLight Last Speaker</a>
</div>
</body>
</html>

the result is


You can simply copy the code and paste it in your VS.NET by creating new page and it will work fine. You don’t need to add scripts manually since I use the scripts directly from Microsoft CDN.

I hope this was helpful.

Regards,
Hajan

NEXT - jQuery Templates - {Supported Tags}

Published Monday, December 13, 2010 9:33 PM by hajan

Comments

# jQuery Templates ??? tmpl(), template() and tmplItem() - Hajan's Blog

Pingback from  jQuery Templates ??? tmpl(), template() and tmplItem() - Hajan's Blog

# Twitter Trackbacks for jQuery Templates ??? tmpl(), template() and tmplItem() - Hajan's Blog [asp.net] on Topsy.com

Pingback from  Twitter Trackbacks for                 jQuery Templates ??? tmpl(), template() and tmplItem() - Hajan's Blog         [asp.net]        on Topsy.com

# re: jQuery Templates – tmpl(), template() and tmplItem()

Tuesday, December 14, 2010 1:59 PM by Erik

When you say "compiled" regarding named templates, what do you mean? In what way is the template compiled?

# re: jQuery Templates – tmpl(), template() and tmplItem()

Tuesday, December 14, 2010 2:52 PM by hajan

@Erik, thank you for your question.

If you pass markup string to $.tmpl() or tmpl() for rendering, the markup will be re-compiled every time it's processed. On the other hand, when using named templates or if you call the template using $("#templateId").tmpl(dataToRender) the template will be automatically compiled and cached, however with template() function you have it compiled by name and you can reuse it easily by its name.

You can read more about this on the following two links:

api.jquery.com/jQuery.template

api.jquery.com/template

# Introduction to jQuery Templates - Hajan's Blog

Tuesday, December 14, 2010 4:38 PM by Introduction to jQuery Templates - Hajan's Blog

Pingback from  Introduction to jQuery Templates - Hajan's Blog

# jQuery Templates with ASP.NET MVC

Thursday, December 16, 2010 4:09 PM by Hajan's Blog

In my three previous blogs, I’ve shown how to use Templates in your ASPX website. Introduction to jQuery

# jQuery Templates – tmpl(), template() and tmplItem()

Monday, December 20, 2010 10:53 AM by progg.ru

Thank you for submitting this cool story - Trackback from progg.ru

# the rasx() context &raquo; Blog Archive &raquo; &ldquo;JavaScriptMVC 3.0: Good To Go!&rdquo; and other links&hellip;

Pingback from  the rasx() context  &raquo; Blog Archive   &raquo; &ldquo;JavaScriptMVC 3.0: Good To Go!&rdquo; and other links&hellip;

# jQuery Templates with ASP.NET MVC &laquo; Chandara

Sunday, January 02, 2011 4:56 AM by jQuery Templates with ASP.NET MVC « Chandara

Pingback from  jQuery Templates with ASP.NET MVC &laquo; Chandara

# jQuery Templates - {Supported Tags} - Hajan's Blog

Thursday, January 13, 2011 4:14 PM by jQuery Templates - {Supported Tags} - Hajan's Blog

Pingback from  jQuery Templates - {Supported Tags} - Hajan's Blog

# Mvc ile jQuery Template Kullan??m??na Giri?? &#8211; Video | asp.net, jquery ve di??er web teknolojileri ??zerine

Pingback from  Mvc ile jQuery Template Kullan??m??na Giri?? &#8211; Video | asp.net, jquery ve di??er web teknolojileri ??zerine

# Mvc ile jQuery Template Kullan??m??na Giri?? &#8211; Video | asp.net, jquery ve di??er web teknolojileri ??zerine

Pingback from  Mvc ile jQuery Template Kullan??m??na Giri?? &#8211; Video | asp.net, jquery ve di??er web teknolojileri ??zerine

Leave a Comment

(required) 
(required) 
(optional)
(required)