[Web]
Ajax with JavaScript Templates
[ Aslak Hellesoy ] 19:54, Saturday, 26 February 2005

Rich, responsive webapps using the Ajax approach are chic. I've started to think about what it would be like to develop the view part of a webapp using the Ajax concepts.

In an Ajax web app the server sends back XML, and the XML is transformed to HTML in the browser. There are several ways to do this transformation:

One approach is to use XSLTProcessor as described here.

However, XSLT is problematic for a couple of reasons. First, not all browsers support it, and more importantly, most web ui designers don't speak XSL.

A second approach is to use XPath in JavaScript to pull out bits of pieces of the XML and modify small pieces of the DOM to fill them in.

A third (and new?) approach could be to use the familiar template approach (as in JSP, ASP, ERB, PHP templates) and generate bigger chunks of DHTML. This would make it much easier to develop nice looking views, since they can now be designed in a regular web authoring tool such as DreamWeaver or NVU.

But wait - the template engine now has to sit inside the browser! And this is where I discovered JavaScript Templates.

JST templates could theoretically be rendered (and tested) in a build system, using Rhino. Being able to test view rendering is something I consider very important, especially when systems start to get complex (which they do with Ajax).

I'm eager to try this out.


Comments

Now add AOP to Javascript and the Ajax approach gets even more powerful.

An example approach is at: http://www.jroller.com/page/deep/20030701

J. Betancourt

--J. Betancourt, February 27, 2005 10:09 PM

J. thanks for the pointer. Very interesting.

Any suggestions about what you'd do in the pre/post/around advices in the context of Ajax? Would this be a good place to implement the logic hitting the server?

--Aslak Hellesoy, February 27, 2005 11:06 PM

In most apps, a combination of techniques might be best. You don't *need* to send XML back to the browser, you could just send strings. If I want to do something dead simple on a page like change the name of a user, all I really need back from the server is a succeed/fail message, not a big hunk of XML. OTOH, if I do something that gets a big dataset back, it's much better to get it as a nice structured peice of XML.

--Andrew, February 28, 2005 03:43 PM

Good idea! I look forward to try this out too. Do you know of any development in this area?

--lisa, February 13, 2006 05:39 AM

I've been working on a replacement to JST, EJS (http://embeddedjs.com).

It works with any javascript syntax (there is no special language to do for loops).

Almost all functionality is performed with 1 line.

It can use JS lint to tell you which line you have errors on.

--Justin Meyer, December 18, 2007 04:19 PM
Post a comment









Remember personal info?