Basics of jQuery Templates

Basics of jQuery Templates

jQuery templates are used to display, manipulate and format data in the browser that we retrieve via jQuery Ajax calls, Page Methods and so on. jQuery templates are sometimes referred to as a client-side repeater and it is somewhat equivalent to an ASP.NET repeater control. jQuery templates were developed by Microsoft in collaboration with the jQuery team.

To demonstrate jQuery templates, I have created a small web application. In this web app, I am simply creating some custom Employees data at the server side and on button click fetching that data and binding it to a HTML table using a jQuery template. The following is the procedure involved.

Step 1

We need to add the base jQuery library file and jQuery template files.

default asp page

Step 2

Add a class file Employee.cs to the project that will act as the custom data type.

Step 3

Add a webpage Default.aspx and add a simple method that returns a list of employees:

  1. [WebMethod]  
  2.         public static List<Employee> GetEmployees()  
  3.         {  
  4.             List<Employee> employees = new List<Employee>   
  5.             {  
  6.                 new Employee { EmployeeId = 1, EmployeeName = "Pam"},  
  7.                 new Employee { EmployeeId = 2, EmployeeName = "Tod"},  
  8.                 new Employee { EmployeeId = 3, EmployeeName = "Alex"},  
  9.                 new Employee { EmployeeId = 4, EmployeeName = "Richard"},  
  10.             };  
  11.   
  12.             return employees;  
  13.         }  
Please note this method is decorated with the “WebMethod” attribute because we will invoke this method using jQuery Ajax. Also, I have hard-coded some employees but in reality we can fetch this data from a database, XML files and so on.

Step 4

Create Mark-Ups:
  1. <table id="Container" border="1px solid black;">  
  2.             <tr>  
  3.                 <th>  
  4.                     Employee ID  
  5.                 </th>  
  6.                 <th>  
  7.                     Employee Name  
  8.                 </th>  
  9.             </tr>  
  10. </table>  
  11.   
  12. <script id="EmployeeScript" type="text/x-jquery-tmpl">  
  13.             <tr>  
  14.                 {{if EmployeeId == 4 }}  
  15.                 <td colspan="2">  
  16.                     Invalid Employee!  
  17.                 </td>  
  18.                 {{else}}  
  19.                 <td>  
  20.                     <label>${EmployeeId}</label>  
  21.                 </td>  
  22.                 <td>  
  23.                     <label>${EmployeeName}</label>  
  24.                 </td>  
  25.                 {{/if}}  
  26.             </tr>  
  27. </script>  
Here, the first mark-up is a straight-forward static HTML table, the second script component will act as the repeater where we will bind our data retrieved from the server. We can add normal HTML contents in this script tag and the values should exactly match that of the property name returned by the server, this is enclosed within special ${ } symbols. Apart from this we can put some conditions based on the output received (here for demo I am showing the employee as invalid if his employee id is 4).

Continued…

Step 5

Finally bind the data by calling WebMethod using jQuery Ajax:
  1. $(document).ready(function () {  
  2.             $('#btnFetchData').click(function () {  
  3.                 BindEmployeesData();  
  4.             });  
  5. });  
  6.   
  7.         function BindEmployeesData() {  
  8.             $.ajax({  
  9.                 type: "POST",  
  10.                 url: "Default.aspx/GetEmployees",  
  11.                 data: "{}",  
  12.                 contentType: "application/json; charset=utf-8",  
  13.                 dataType: "json",  
  14.                 success: function (response) {  
  15.                     if (response.d && response.d.length > 0) {  
  16.                         $("#EmployeeScript").tmpl(response.d).appendTo("#Container");  
  17.                     }  
  18.                 },  
  19.                 error: function (ex) {  
  20.                     alert("error");  
  21.                 }  
  22.             });  
  23.             return false;  
  24.         }   
Please note the highlighted line here, when we receive the response from the server, we are simply appending the response (that is present in response.d) to the Container table using the “tmpl” method of the jQuery template.

Output

jQuery templet demo

A jQuery template Item has the following properties and methods: 
  • data: The data associated with the Template Instance. For example, a product.
  • tmpl: The template associated with the Template Instance.
  • parent: The parent template item if the template is nested.
  • nodes: The HTML content of the template.
  • calls: Used by {{wrap}} template tag.
  • nest: Used by {{tmpl}} template tag.
  • wrap: Used to imperatively enable wrapped templates.
  • html: Used to filter content from a wrapped template. See the preceding section on wrapped templates.
  • update: Used to re-render a template item.

Advantages

  1. Easy to use.
  2. We can write the content more declaratively than programmatically.
  3. We can reuse the templates in various scenarios.

Disadvantages

  1. If a template is large then it its execution can become excessive, resulting in slow response.

Up Next
    Ebook Download
    View all
    Learn
    View all