In preparation for my <a href="http://blog.reybango.com/wp-content/uploads/2010/07/tv-jquery-con.png">upcoming talk on jQuery Templates</a>, I've been been deep diving into the <a href="http://github.com/nje/jquery-tmpl">jQuery Template plugin created by Microsoft.</a>. Many of you are probably familiar with server-side template engines like Smarty or Cheetah but only recently have client-side template engines taken off. Considering how complex our web apps are becoming, it makes total sense.

The cool thing about templates is that it lets you easily structure your content display without all the hassle of string concatenation. For example, say I had a data set containing an ID and a name and I wanted to display the name as a hyperlink.

I could do something like this:

[code language="js"]
var clRec = &quot;&quot;;

for(var i=0; i&lt;client.name.length; i++) {
    clRec += &quot;&lt;li&gt;&lt;a href='clients/&quot;+client.id[i]+&quot;'&gt;&quot; + client.name[i] + &quot;&lt;/a&gt;&lt;/li&gt;&quot;;
}
[/code]

but to me, this seems much more readable and maintainable:

[code language="js"]
&lt;li&gt;&lt;a href=&quot;clients/${id}&quot;&gt;${name}&lt;/a&gt;&lt;/li&gt;
[/code]

The second code snippet is a template I've created to render a hyperlinked name in a list and it's very easy to do. First, include both jQuery and the jQuery Template plugin into your page:

[code language="js"]
&lt;script src=&quot;http://code.jquery.com/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;jquery.tmpl.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;  
[/code]

Next, define the data that you'd like to see rendered. This could be data derived from an XHR call or something you've predefined, like this:

[code language="js"]
	var clientData = [
		{ name: &quot;Rey Bango&quot;, age: &quot;32&quot;, id: 1 },
		{ name: &quot;Mark Goldberg&quot;, age: &quot;51&quot;, id: 2 },
		{ name: &quot;Jen Statford&quot;, age: &quot;25&quot;, id: 3 }
	];
[/code]

Now, define your template:

[code language="js"]
&lt;script id=&quot;clientTemplate&quot; type=&quot;text/html&quot;&gt;
	&lt;li&gt;&lt;a href=&quot;clients/${id}&quot;&gt;${name}&lt;/a&gt;&lt;/li&gt;
&lt;/script&gt;
[/code]

Yep, the "script" tags are necessary and used because they allow your template to be embedded within the body of the page. Notice that we've defined the placeholders as ${id} & ${name}, the same names used in the identifiers in our data. The "${}" tells the template parser that the field needs to be replaced with the expected value.

Then call the jQuery Template plugin to render the data using your template: 

[code language="js"]
$(&quot;#clientTemplate&quot;).tmpl(clientData).appendTo( &quot;ul&quot; );
[/code]

The plugin's <em>.tmpl()</em> method accepts the data we've defined and handles the parsing of the template we've selected. Then we use jQuery's <em>.appendTo()</em> method to append the results to an unordered list tag.

That's it! Told you it was simple. :) 

I'll go into more advanced details during my presentation but this should give you something to start with in the meantime. Here's the full source for you to use:

[code language="html"]
&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt; 
  &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt; 
  &lt;meta name=&quot;robots&quot; content=&quot;noindex&quot; /&gt; 
  &lt;title&gt;Canvas Test&lt;/title&gt; 
  &lt;script src=&quot;http://code.jquery.com/jquery.min.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
  &lt;script src=&quot;jquery.tmpl.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;  
  &lt;script type=&quot;text/javascript&quot;&gt;
  $(document).ready(function() {

	var clientData = [
		{ name: &quot;Rey Bango&quot;, age: &quot;32&quot;, id: 1 },
		{ name: &quot;Mark Goldberg&quot;, age: &quot;51&quot;, id: 2 },
		{ name: &quot;Jen Statford&quot;, age: &quot;25&quot;, id: 3 }
	];


$(&quot;#clientTemplate&quot;).tmpl(clientData).appendTo( &quot;ul&quot; );


});
  &lt;/script&gt;

 &lt;script id=&quot;clientTemplate&quot; type=&quot;text/html&quot;&gt;
	&lt;li&gt;&lt;a href=&quot;clients/${id}&quot;&gt;${name}&lt;/a&gt;&lt;/li&gt;
&lt;/script&gt;
  
&lt;/head&gt; 
 
&lt;body&gt; 

&lt;ul&gt;&lt;/ul&gt;

&lt;/body&gt; 
&lt;/html&gt; 
[/code]