Sunday, August 19, 2007

javascript for templates - it's happening.

It seems like javascript for template languages might be the way forward.

  • designer friendly. lots of web designers know a little javascript or actionscript.
  • can run in a browser.
  • javascript can be sandboxed.
  • most web developers know a little js too. Web developers that don't know javascript will most likely at know at least a little C/php/perl/java to be able to do a lot of things with javascript.

    Tenjin allows javascript templates. It also allows many other scripting languages to be used in templates... but that's not really what I'm talking about.

    Fast, small(200k), opensource and widely deployed javascript/emca script interpreters exist. Tamarin is a emca JIT optimized virtual machine from flash 9, that mozilla is using in upcoming versions of firefox. There is also spidermonkey - the current firefox javascript implementation. Finally there is haxe, which is a emca script like language - that can output .js files, flash swf files, and also neko which runs as a virtual machine in apache. So it would be possible to even use your templates in flash, as well as in normal html browsers.

    However you don't need to use javascript in your templates in order to convert the templates into javascript. Genshi2js can convert genshi templates into javascript, so you can run your templates in a browser, or server side. This shows that you don't need to use the javascript language in order to gain the benefit of using your templates in the browser.

    Language agnostic template languages are important - because a wider selection of people can edit your templates. Javascript usage in the template allows you to get that wider selection of people being able to edit your templates. The genshi2js solution is quite nice, and it proves that you can still compile other templates languages to be executed in the browser - however it is not known by as many people as a javascript.

    An exciting possibility is for applications which serve dynamic pages via haxe->mod_neko with compiled tenjin templates. This would seem like a very fast way to generate dynamic pages from json data sources. The json could quite easily be generated in any language - including python, static files, or any webservice through mod_proxy.

    I'm sticking with python, and php on the server side for the time being - not switching to emca script server side. Python because I like coding python, and php because it is widely installed (and there seems to be more work available). However using javascript for the templates might just be an option I can live with. Especially if I can compile the javascript using templates into php, and python.

    It all depends on the project of course. But I'm definitely considering genshi (with genshi2js), and tenjin for future projects.

    philhassey said...

    hah - what are the odds .. just 3 days ago I started using and had to implement an ultra-simple javascript based templating engine. works quite nicely :)

    philhassey said...

    p.s. the way I did it was to use pure HTML for the template .. with stuff like
    <h1 id='title'>title goes here</h1>

    and then had a skin.js that did the document.getElementById('title').innerHTML = ....;