Anand Chitipothu

JavaScript templating with web.py

05 Mar 2010 – Visakhapatnam

I’ve been looking for a good JavaScript templating library for so long. I was using a variant of JavaScript Micro-Templating for my work and I wasn’t happy with it. Most often, I ended up in duplication because of writing a Python template and a JavaScript template for doing the same thing.

I started exploring to see if it is possible to extend Templetor, the web.py templating engine, to solve this issue. To my surprise, it turned out to be very nice.

I added a new construct jsdef to Templetor. It defines a template function and also generates an equivalent JavaScript function.

For example:

$jsdef hello(name):
    Hello, $name!
    
$hello("world")

will generate:

<script type="text/javascript">
function hello(name){
    var self = [], loop;
    self.push("Hello, "); self.push(websafe(name)); self.push("!\n");
    self.push("\n");
    return self.join("");
}
</script>
Hello, world!

The jsdef block does two things here. It defines a template function that can be used while rendering the template and generates an equivalent JavaScript function that can be used at the client.

There are some caveats though. This is just an attempt to drive JavaScript templating from Python. But that won’t magically happen because Python is not JavaScript.

It is important to remember that not all functionality in Python is supported. Features like multiple assignments, list slicing, list comprehensions won’t work here. If you are using a python function inside the jsdef block, you need to provide the equivalent in JavaScript too. Some builtin functions of Python and other utilities used by the generated code are provided along with this extension.

Code is at http://github.com/anandology/notebook/tree/master/2010/03/jsdef/.

blog comments powered by Disqus
Fork me on GitHub