Steve Wellens

Programming in the .Net environment

Sponsors

Links

Goodby jQuery Templates, Hello JsRender

A funny thing happened on my way to the jQuery website, I blinked and a feature was dropped: jQuery Templates have been discontinued. The new pretender to the throne is JsRender.

jQuery Templates looked pretty useful when they first came out. Several articles were written about them but I stayed away because being on the bleeding edge of technology is not a productive place to be. I wanted to wait until it stabilized…in retrospect, it was a serendipitous decision.

This time however, I threw all caution to the wind and took a close look at JSRender. Why? Maybe I'm having a midlife crisis; I'll go motorcycle shopping tomorrow.

Caveat, here is a message from the site: Warning: JsRender is not yet Beta, and there may be frequent changes to APIs and features in the coming period.

Fair enough, we've been warned.

The first thing we need is some data to render. Below is some JSON formatted data. Typically this will come from an asynchronous call to a web service. For simplicity, I hard coded a variable:

    var Golfers = [
        { ID: "1", "Name": "Bobby Jones", "Birthday": "1902-03-17" },
        { ID: "2", "Name": "Sam Snead", "Birthday": "1912-05-27" },
        { ID: "3", "Name": "Tiger Woods", "Birthday": "1975-12-30" }
        ];

We also need some templates, I created two. Note: The script blocks have the id property set. They are needed so JsRender can locate them.

    <script id="GolferTemplate1" type="text/html">
        {{=ID}}: <b>{{=Name}}</b> <i>{{=Birthday}}</i> <br />
    </script>
 
    <script id="GolferTemplate2" type="text/html">
        <tr>
            <td>{{=ID}}</td> 
            <td><b>{{=Name}}</b></td> 
            <td><i>{{=Birthday}}</i> </td>
        </tr>
    </script>

Including the correct JavaScript files is trivial:

    <script src="Scripts/jquery-1.7.js" type="text/javascript"></script>
    <script src="Scripts/jsrender.js" type="text/javascript"></script>

Of course we need some place to render the output:

    <div id="GolferDiv"></div><br />
    <table id="GolferTable"></table>

The code is also trivial:

    function Test()
    {
        $("#GolferDiv").html($("#GolferTemplate1").render(Golfers));
        $("#GolferTable").html($("#GolferTemplate2").render(Golfers));
 
        // you can inspect the rendered html if there are poblems.
        // var html = $("#GolferTemplate2").render(Golfers);
    }

And here's what it looks like with some random CSS formatting that I had laying around.

 

 Not bad, I hope JsRender lasts longer than jQuery Templates.

One final warning, a lot of jQuery code is ugly, butt-ugly. If you do look inside the jQuery files, you may want to cover your keyboard with some plastic in case you get vertigo and blow chunks.

[Edit]

Someone asked me if it was possible to include the templates in an external file.  This seemed to work: 

<!--#include virtual="Scripts/RenderTemplates.js" -->

I hope someone finds this useful.

Steve Wellens

Comments

Mike said:

They should have adopted a more lean syntax, like Razor.

# November 10, 2011 6:09 AM

HeartattacK said:

Hi Steve,

That looks pretty much like jQuery templates...why would somebody wish to use jrender instead of it? Knockout uses jQuery templates for rendering (while the latest version is going to have its own templating engine) - is there a compelling reason for jsrender?

# November 10, 2011 6:52 AM

RichardD said:

Shouldn't the template blocks have a type of "text/html" (or "text/x-jquery-tmpl" as the samples use)? If you leave it as "text/javascript", the browser will try to execute them.

# November 10, 2011 9:45 AM

SGWellens said:

    >> That looks pretty much like jQuery
    >> templates...why would somebody wish to
    >> use jrender instead of it?

Because jQuery templates are no longer supported.

Here's the link: github.com/.../jquery-tmpl

Oh yeah, and they are much faster.

# November 10, 2011 10:21 AM

SGWellens said:

>>Shouldn't the template blocks have a type of
>>"text/html"(or "text/x-jquery-tmpl"
>>as the samples use)?
>>If you leave it as "text/javascript",
>>the browser will try to execute them.

RichardD, you are one sharp dude.

When I used "text/x-jquery-tmpl" I got an HTML 5 validation error saying, "The values permitted for the attribute do not include "text/x-jquery-tmpl".  

What I had worked in IE and FF.  But, I tried using "text/html" as you suggested and it also worked.  I updated my post.  Thanks friend!

# November 10, 2011 10:34 AM

Sam said:

jqTemplates was better, very disappointed this was dropped.  

# November 10, 2011 4:16 PM

bcs said:

Nice one.

How will you give column headings and border?

# November 21, 2011 6:01 AM

SGWellens said:

You have complete access to the HTML to add headings and of course CSS to add borders.

# November 21, 2011 8:11 AM

cellvia said:

sorry I had to mention, this cracked me up

"This time however, I threw all caution to the wind and took a close look at JSRender. Why? Maybe I'm having a midlife crisis; I'll go motorcycle shopping tomorrow."

thanks for the tut

# December 10, 2011 7:00 AM

John.xu said:

Thank for your share..

# December 18, 2011 9:31 PM

Bruno said:

Do you know what?

The new outlook.com implements Jquery template.

Take a loot at the source code.

You going to see what I'm talk about.

# August 22, 2012 9:16 AM