Anand Chitipothu

JavaScript templating with

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 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!

will generate:

<script type="text/javascript">
function hello(name){
    var self = [], loop;
    self.push("Hello, "); self.push(websafe(name)); self.push("!\n");
    return self.join("");
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

Fork me on GitHub