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.

Comments

  • Pingback: SitePen Blog » Introducing the Dojo Toolkit 1.1()

  • Dojo on the JS server side is also available with the Phobos project (https://phobos.dev.java.net) since December 2006:

    Read https://phobos.dev.java.net/tutorials/usedojo/

    Ludo

  • @Ludo: I agree that Phobos is a great and promising project, but it looks like it is not being actively developed currently. I hope I am wrong on this, but that’s my current understanding.

  • Pingback: DTL mit Zend Framework und Dojo Toolkit - dojotoolkit-forum.de()

  • Pingback: SitePen Blog » Jaxer + Persevere via Dojo’s JsonRestStore()

  • Karthik

    “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.”

    I have noticed that an app built solely with dojo client side widgets takes a while to load (before the UI becomes responseive).
    I was wondering if I could use jaxer and get the initial dojo widget rendering on the server – do you think this would speed up the initial render? – is the server-side JS faster than client side?

    but from your post, it does look like you cannot offload the rendering work to the server?

    – Karthik

  • @Karthik: It would be interesting to see if that is possible and beneficial. Jaxer is essentially running Firefox on the server; it is not necessarily much faster than the client except it doesn’t do the visual rendering (which the browser has to do regardless of where the HTML was generated) and it has local access to the file system, so I am not sure. If you a large number of users connecting to your system, it may still be preferable to have the processing of widgets distributed on to the browsers rather than trying to do it all on the server for scalability.