Home > ASP.NET, C#, HTML, JavaScript, Programming, Tips & Tricks, UI, jQuery > jTemplates – jQuery Template Engine

jTemplates – jQuery Template Engine

About a year ago i had to architucture some web application with a very rich client side UI.

ASP.NET was an easy choise for a server side code, but what about client side ?
The heavy viewstate, endless event handlers and infinity number of server side requests just to make the GUI  friendly – well, it just didn’t sound as a proper way to do it.

So, what do we have ?
.NET as a server side platform, but despite  all it’s powerful abilities it has nothing to do with client side. At the end of the day all those fancy control are just HTML and JavaScript.

JavaScript is still the engine that runs at the client side and it’s great, but we all know how many lines of code we have to write and time to spend to make it fast, modular and cross browser.

jQuery – it’s a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.

Indeed, we can do a many things with jQuery and it realy simplify the coding, but what if we have to build a product list with complicated HMTL, complex logic and real time manipulation ?

As i mentioned before, ASP.NET and UpdatePanels are just to heavy and JavaScript is to long.

So, i did some research and find a couple of JavaScript Template Engines.
The first one i bumped in was jsRepeater. At first it seemed like a great solution but after a few tests i found all kinds of bugs related to the syntax conflicts with HTML and JavaScript.

After a few days of googling, tests and googling again i found the one and only –  jTemplates.
jTemplates is a template engine for JavaScript, wich is wraped in a jQuery Plugin.
The syntax is very simple and  intuitive. It’s support parameters, includes, if/else, loops, JavaScript functions and many other nice and usable features.

Let’s say we want to list all our products. What we have to do is to write the template (HTML), pass the data and we have a product list.

{#foreach $T.Product as P}
<div style=”float:left;width:150px;height:150px”>
<div>{$T.P.Title}</div>
<img src=”{$T.P.Url}alt=”{$T.P.Title}” style=”width:10px;height:10px” />
<hr />
<div>Price : {$T.P.Price}$</div>
</div>
{#/for}

Look how easy this is. You just write HTML and werever you need to insert some data you put {$.T.P.ColumnName}.

So, how we use it ?
Just add some textarea to your page, give it some id, set it’s display style to none and write your template inside.

<textarea cols="0" rows ="0" style="display:none;" id="ProductListTemplate"><!--
{#foreach $T.Product as P}
	<div style=”float:left;width:150px;height:150px”>
		<div>{$T.P.Title}</div>
		<img src=”{$T.P.Url}” alt=”{$T.P.Title}” style=”width:10px;height:10px” />
		<hr />
		<div>Price : {$T.P.Price}$</div>
	</div>
{#/for}
--></textarea>

Add some empty container that will hold the result.

<div id="ProductListContainer"></div>

Execute setTemplateElement and execute processTemplate while passing the data into it as a parameter

$("#ProductListContainer").setTemplateElement("ProductListTemplate");
$("#ProductListContainer").processTemplate(data);

Easy, isn’t it ?
I must say that the above example is a simple one, but I’ve been using jTemplates to create complicated lists, grids, trees, popups and even an image editing web application. Use it, I’m sure you will love it.


VN:F [1.7.7_1013]
Rating: 4.9/5 (10 votes cast)
jTemplates - jQuery Template Engine4.9510

A few posts you might find interesting:

  1. IE Memory Leak – jQuery Fix
  2. Javascript function context, “apply” method and lost “this” object
  3. Script as blocking element and dynamic script loading using document.write
  4. Javascript pseudo protocol (is EVIL)
  5. SpeedTrace – .NET Profiler and Tracer

  1. October 4th, 2009 at 20:20 | #1

    interesting ! do you use ASP.NET MVC or webForms for server-side ?

    UN:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
  2. October 4th, 2009 at 22:25 | #2

    @Amr jTemplates is a client side engine, so server side technology is really your choice and of course you don’t have to use .NET.
    BTW it’s syntax is slightly reminds the ASP.NET AJAX 4.0 Templates.

    UA:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
  3. October 4th, 2009 at 22:55 | #3

    @Xander
    I was just wondering what do you have used for server-side with jTemplates

    UN:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
  4. October 4th, 2009 at 23:45 | #4

    @Amr , in my last project it was ASP.NET WebForms, but I’ve been using it in all kind of sites: ASP 3.0, PHP…

    UA:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
  5. October 6th, 2009 at 15:31 | #5

    Is there a template engine which works (in two implementation) server AND on client-side?

    idea: First website requests render the html on server side, all other requets can be handled with JS and render on client side.

    Thanx for the jtemplate-tipp.

    UN:F [1.7.7_1013]
    Rating: 5.0/5 (1 vote cast)
  6. October 6th, 2009 at 17:37 | #6

    @Christian Harms I don’t know one and i don’t think it’s possible because of the huge differences between two, but the idea is nice.

    UA:F [1.7.7_1013]
    Rating: 0.0/5 (0 votes cast)
  1. No trackbacks yet.

Subscribe without commenting

SEO Powered by Platinum SEO from Techblissonline