Click here to Skip to main content
13,248,766 members (54,676 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


7 bookmarked
Posted 17 Feb 2011

jQuery Templates

, 17 Feb 2011
Rate this:
Please Sign up or sign in to vote.
jQuery Templates

I am a big fan of jQuery, as it is my favorite JavaScript framework. It has many great features already, and even more are being developed everyday. It simply makes developing responsive web applications much, much faster.

I am currently working on a large application that displays to the user a lot of grids practically on every screen. The data inside the grids, as well as their basic structure can change according to different selections the user makes. It is very important for me to make this web application very responsive and fast, so it was pretty obvious from the start that I'd be using a lot of Ajax and depending heavily on the client to do the rendering.

I had a standard Ajax call, returning me a JSON object to work with. I then found myself trying to dynamically build a string that would represent the HTML markup inside a table, appending it to the table as I iterate over the JSON properties. This isn't very difficult to do at first, but will always look very messy, and will be much uglier to maintain...

Luckily, just in time, I came across the jQuery documentation of their new templates feature. This allows you to design a small template of HTML, mark where the parameters will be, and bind a JSON object to that template. Then, you can practically do whatever you want with it- append it to a table, or just use it as a list of data, and the best part is that the UI is separated from the 'code', meaning it is very easy to maintain since you can change your template around freely and easily, while never changing the Ajax calls nor JavaScripts.

I'll show some simple examples so you can see what I mean, and to help get you started on your own.

First, you obviously need to include the templates plug-in into your HTML file (can be found here):

<script language="javascript" type="text/javascript"

<script language="javascript" type="text/javascript"


The template you want to use could be inserted into a script tag like this:

<script id="templateStructure" type="text/x-jquery-tmpl">

Notice that I gave the script an id, which we'll need soon, and the type is marked text/x-jquery-tmpl.

The ${} brackets tell the jQuery where to place the data of the JSON object we will bind. The name inside the brackets must correlate with the properties of the bound JSON object. This means our JSON object will be an array of objects (or one object if that's all we have) that all have the properties FirstName, LastName and Email.

The template I created represents a row in a table that I will bind to, and the table I'm going to bind it to, looks like this:

<table border="1" id="templateTable">
        <td><b>First Name</b></td>
        <td><b>Last Name</b></td>

So when I add the rows, the table will be filled with data. In order to load the template, I use the template() method :


This will load the template we defined in the script tag and call it myTemplate.

Now, all we need to do is give the template a data source, and place it wherever we want. In our case, we'll append it to the table so it 'fills' the table's data.

$(document).ready(function() {
    // our data object we will bind
    var myData = [ {FirstName:'Bob', LastName:'Jannovitz', Email:''},
                   {FirstName:'Howard', LastName:'Shennaniganz',
                   {FirstName:'Joe', LastName:'Stoozi', Email:''} ];

    // load the template and name it 'myTemplate'

    // bind the data to the template and append to the table
    $.tmpl('myTemplate', myData).appendTo('#templateTable');

The final result will be the table with the data rendered into it.

In Conclusion

The templates jQuery plug-in can be extremely useful in binding data to an HTML template on the client for fast responsive applications. It also has many more great features like instructions that can cause your template to act differently to different data situations. It is important to note though, that all this is only in beta stage, and is subject to change. I however, already started using it, and so far so good... :)

I might be posting something more advanced about this soon, but until then - you can read more about it here.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Gilly Barr
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# &, while serving in the IDF.
Worked on the 'Core Performance' Team at (Sears Israel)
Currently working at

Check out my blog!
or my twitter

You may also be interested in...

Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 17 Feb 2011
Article Copyright 2011 by Gilly Barr
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid