Jaxer and the Dojo Toolkit’s DTL

By on February 29, 2008 1:22 pm

Using Dojo’s Django Template language on Jaxer

Aptana’s Jaxer is a server side JavaScript (SSJS) server. With some modifications to a web page, the Dojo Toolkit 1.1 will be able to run on the server. With the Dojo Toolkit running on the server, you can utilize the Dojo Toolkit’s Django Template library (DTL) rendering engine to do templating within Jaxer. The current build of the Dojo Toolkit does not yet include support for Jaxer, but support is currently being added and future builds should include patches to properly work with Jaxer. Once Jaxer support has been added to the Dojo Toolkit, the following modifications will be needed for your page to run on the Jaxer server:

You must explicitly set the base URL of the Dojo Toolkit library. Jaxer does not provide the ability for the Dojo Toolkit to auto-detect the base URL as it can in other environments. Therefore you must declare the base URL with the djConfig global variable:

Next, you must add the runat attribute with a value of “both” to all of the script tags that you want executed on the server and the client. Your script tags should look like:

Last, you must include dojo.require(“dojo.jaxer”) in a script tag with a runat=”server” attribute, in order to load the Jaxer specific module that is necessary for Dojo to run on Jaxer. This should immediately follow the declaration of dojo.js:

Your script tags should now look like:

Once this is done, Dojo should load in Jaxer, and you can utilize the library capabilities of the Dojo Toolkit on the server side. In particular, you can now use the DTL renderer as you would on the browser. The DTL renderer can take templates written using Django template language and render the templates based upon JSON data. If you are running Jaxer, you can view a demonstration of DTL rendering on the server by loading /dojox/dtl/demos/demo_Templated_Jaxer.html (make sure the Dojo Toolkit base URL is correct).

By using Jaxer’s capability to allow scripts to execute on both the client and server, we can utilize our Dojo Toolkit in both environments. On the server we can utilize many of the Dojo Toolkit’s features such as functional language features, math libraries, cryptography, dates, encoding, and more to improve development speed and code quality. We can use the same powerful features and idioms in both the browser and the server. An example of a practical way to use the Dojo Toolkit in both environments is to utilize the DTL renderer as described above to generate HTML on the server, and then use the Dojo Toolkit’s Ajax capabilities to dynamically update the page once it’s on the browser. For example: we could use the DTL renderer to display the comments in a blog, dynamically add any new comment the user makes at the end of the list of comments, and then send the comment to the server using Ajax.

It is important to note that Jaxer is not capable of transferring the programatically set event handlers for widgets—it can only send the static HTML to the browser. This means you can use DTL as a templating engine to create HTML on the server, but the Dojo Toolkit client side widgets are still necessary if you want to use interactive widgets on the browser.