Techniques Behind Modern Web
8 Aug
The idea of utilizing any part of your HTML document as template to render data purely with JavaScript and CSS has been realized with Pure library.
While nothing to prevent you from using server-side template engine like Smarty, it does not help much if your application is heavily ajaxified and many parts of the app are built after the “onload” event with data pulled behind screen. For example, a list of players is to be rendered just after user selects the team name. One (dirty) solution is to do everything from server side and assign pre-formatted HTML to DOM element like:
var preFormattedHTML = getHTMLByAjax(); // get pre-formatted HTML from server via Ajax var el = document.getElementById(‘target’); el.innerHTML = preFormattedHTML; // ...
OK, this approach works (though strictly MVC-minded people may not like it) but you are putting unnecessary workload on server while this can be undertaken all from client-side.
“Pure” is not the first attempt to create a Javascript/HTML template engine, you can find some other solutions on the net which are in independent projects or as part of an Ajax framework. However, Pure looks like the most comprehensive, systematic and flexible way out.
Basically, to render data, you need a piece of HTML as template, a context which contains data to render, a directive to tell how to render and of course a target element. Please go through 4 examples on Pure’s website to see how to create simple to more complicated templates. I also got my hand dirty to create a slightly more advanced example that renders some recommended reading books when user selects a programming language from dropdown control.
First, just build a template table in pure HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <table class="books" id="templ"> <thead> <tr> <th class="title">Title</th> <th class="publisher">Publisher</th> <th class="price">Price</th> </tr> </thead> <tbody> <tr> <td class="title"> </td> <td class="publisher"> </td> <td class="price"> </td> </tr> </tbody> </table> |
Then write a directive to tell “Pure” how to format the result table from data (a list of books):
1 2 3 4 5 6 | var directive = { 'tbody tr': 'book <- list', 'td.title': 'book[0]', 'td.publisher': 'book[1]', 'td.price': 'book[2]', 'tbody tr[class]' : (function(context, items, pos) { return ( pos % 2 == 1 ) ? 'even' : 'odd'; })}; |
And a render function using JQuery:
1 2 3 4 5 | function render(){ $.getJSON('json.php', 'lang=' + $('#langList').val(), function(books){ $('#target').html($('#templ').$pMap(directive).$pRender(books)); }); } |
As you may notice, data (in JSON) are dynamically retrieve based on a language selected via a JQuery Ajax function getJSON().
Finally, here are all 17 lines of JavaScript code used to get it done. Pretty simple, no?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | var directive = { 'tbody tr': 'book <- list', 'td.title': 'book[0]', 'td.publisher': 'book[1]', 'td.price': 'book[2]', 'tbody tr[class]' : (function(context, items, pos) { return ( pos % 2 == 1 ) ? 'even' : 'odd'; })}; function render(){ $.getJSON('json.php', 'lang=' + $('#langList').val(), function(books){ $('#target').html($('#templ').$pMap(directive).$pRender(books)); }); } $(document).ready(function(){ render(); $('#langList').change(render); }); |
Please see online example and download source codes below.
Recommended Reading: This site recommends Website Magazine for 'Net Success
Don't forget to subscribe
so you don't miss out on future posts!
Hi,
Thank you so much for getting your hands dirty with PURE!
Your example is just really good because it demonstrates how real it is and how simple it is.
Our life has become much more easier developing web app using that piece of JS and we wanted to share it, just as many of other great pieces of JS we are using everyday.
If you have any question, just pass by the Google Group. It starts to have some kind of real existence!
http://groups.google.com/group/Pure-Unobtrusive-Rendering-Engine
Doesn’t look like you looked into Jemplate, which is a JavaScript implementation of Template Toolkit, a server-side templating language which is very popular in Perl and has a Python implementation as well. If you already using TT, you can basically use the same template langauge on the server- and client-side.
@rdj: Jemplate looks very ample. Unfortunately I don’t know much about TT to have right evaluation.
Before creating PURE, Jemplate has been very close to be our choice as template engine.
The speed of the pre-compiled JS was amazing.
But we didn’t like having to install Perl to build templates, and the [%..%] tags.
But indeed, the functional richness inherited from TT2 is a great point.
We wanted to create something were the only languages to use were HTML/CSS and JS and to take away the % tags.
Hi Jimmy,
We have just released a new version of PURE, including new features such as :
Auto-rendering: a new PURE method takes your HTML and your JSON data and merges them automatically. The class attribute is used to map the HTML and the data.
Recursive Template Call
The changes are quite important.
We took back your example and changed it to fit with the new release.
The code source can be seen below and as you will see, it is really easier :
http://friendpaste.com/FSBbhtb5
I have a zip file for you with the example if you contact me directly.
Hi, I found your blog on this new directory of WordPress Blogs at blackhatbootcamp.com/listofwordpressblogs. I dont know how your blog came up, must have been a typo, i duno. Anyways, I just clicked it and here I am. Your blog looks good. Have a nice day. James.
Will it beat google or yahoo ?
[...] JavaScript - HTML Template Engine: Pure | Just Talk About Web “Pure” is not the first attempt to create a Javascript/HTML template engine, you can find some other solutions on the net which are in independent projects or as part of an Ajax framework. However, Pure looks like the most comprehensive, systematic and flexible way out. [...]
[...] More than 7000 of you came to discover the tool during the first week and some of you already started using the solution, like Jimmy from the blog Just Talk About Web. [...]
[...] a programming language and display recommended reading books accordantly. This is almost similar to another example I created for PURE [...]