jQuery Templating in the wild
It seems like longer, but it’s only been 6 months since we announced that Microsoft was going to start contributing to jQuery instead of developing our own JavaScript library. Today, Scott Guthrie announced that we’ve had our code checked in to the main branch of the jQuery project – you can read more about that on his blog and also check out the Web Camps TV interview I recorded with Stephen Walther on the subject. John Resig has also confirmed that jQuery Templates will ship as an official plugin in v1.4.3 and then in the core library in v1.5. (Data-linking will also be shipped the same way).
Check out the Web Camps TV interview with myself and Stephen Walther where we demo some of the new features:
Holy miracle, Batman!? Microsoft are contributing to open source!

Yes that’s right, Microsoft are contributing to jQuery, which is an open source project. That means we have real life developers working on shipping code – you can see what they’ve been up to on github. Boris Moore the lead dev on our contributions has been writing up his thoughts on working at Microsoft on an open source project which make for some interesting reading. He also provides some details on using the templating engine and some of the advanced features that all templating ninjas should know.
Why use jQuery Templates?
There are basically two reasons to use jQuery Templates:
- Performance
- Maintainability of your code
On Performance. This benefit comes in two flavors, user experience and bandwidth.
First, let’s discuss user experience. Modern websites are behaving more and more like desktop applications, with data that is constantly being refreshed with no user interaction. Think about Twitter or Facebook and how they are regularly pulling data from the server to provide a refreshed look automatically. jQuery Templates make it simple to declare the shape of your HTML and then insert fresh data to them, appending the resultant HTML to the DOM where appropriate.
Secondly, there’s bandwidth to think about. Chatty applications that send/receive excessive amounts of data are bad for your bandwidth bills and bad for latency. Instead of refreshing the entire page when just one portion needs updating, we can keep the noise down to a minimum with HTTP calls between JavaScript/jQuery and the server. jQuery Templating allows you to take that data and then do something useful with it in an easy way. With this approach you are going to be making fewer calls and moving less data to the client. There’s also the caching to think about as well – templates are cached on the client which will further reduce the data you are moving about.
On Maintainability of your code
Given that you’ve bought into rendering your data on the client-side because of the perf benefits, let’s also assume that your sanity is important too – this is another reason to use jQuery Templates. It is of course possible do the job of jQuery Templates manually, using string concatenation to build up complex strings of HTML and injecting your data items as you go. Rinse and repeat for each row of data you have and then have jQuery append it to your DOM. However, there are more rewarding things in life than doing this, and if you’ve ever tried to match up HTML elements and tried to troubleshoot a complicated nugget of HTML you’ll know it’s pretty painful. This is where templating helps by allowing you to declare a template inline in a code block so you can see the structure of the template at first glance (see examples below). It’s also worth calling out that templates make it easy to separate the logic on the client into app, structure and style, i.e. JavaScript, HTML and CSS. For example, in my applications I like to have all my templates in a JS file called templates which I reference in my app when needed.
Pop quiz: which is easier to understand? This template?
<div>
<div>
<img src="${BoxArt.LargeUrl}" />
</div>
<strong>${Name}</strong>
<p>{{html Synopsis}}</p>
<input type="button" title="Buy tickets for '${Name}'" value="Add to cart…" class="buyButton"/>
<br/>
</div>
</script>
or this concatenated string?
var myString = "<div><div><img src=";
myString += BoxArt.LargeUrl;
myString += "/></div><strong>";
myString += Name;
myString += "</strong><p>";
myString += Synopsis;
myString += "</p><input type=\"button\" title=\"Buy tickets for\"";
myString += Name;
myString += " value=\"Add to cart…\" class=\"buyButton\"/>;
myString += "<br/></div>";
</script>
Hopefully the difference is clear ![]()
Who’s using jQuery Templates?
These bits are pretty fresh so there are only a few examples of websites using them. One early adopter is hiFi, a website publishing engine. They take performance seriously which is why they chose to use jQuery Templating engine to render their comments to the client. The team at hiFi have also written up a really nice post on the subject of jQuery Templates.
To get started with templates I recommend checking out the jQuery API documentation and also the samples that Boris has created on github.com. Rey Bango also has some great tutorials on the different ways you can declare templates too.






Pingback: jQuery: » New Official jQuery Plugins Provide Templating, Data Linking and Globalization
Pingback: jQuery Templates, Data Link, and Globalization Accepted as Official jQuery Plugins - ScottGu's Blog
Pingback: New Official jQuery Plugins Provide Templating, Data Linking and Globalization - Free Web Design Phoenix
Pingback: Templates, Data Link, e Globalization Aceitos como Plugins Oficiais da jQuery - ScottGu's Blog em Português
Pingback: jQuery模板,数据链接及国际化等被吸纳成官方jQuery插件