Michael Kerrin

Web, Python, JavaScript and related technologies

pwt.jinja2js

leave a comment »

I have just released pwt.jinja2js This is my compiler for Jinja2 templates that compiles to Java Script. If anyone noticed I renamed the project from pwt.jscompiler that I previously announced on this blog. But pwt.jinja2js is now released to PyPI which should make it easier to use and fixes the name in stone now.

I started this project so that I could easily use templates in my Java Script code. Most other Java Script templates require web developers to include the template they want to render inside the HTML returned from the server. This is annoying as you have to remember to include both Java Script and your HTML snippet in the response, increasing the load of the HTML page resulting in slower website. I found and used Closure templates for a while. This does what pwt.jinja2js does and inspired me to work on this project. But closure templates is a Java project and the only way to use it in a Python project is via the command line which is annoying.

pwt.jinja2js works by compiling any macros found inside your Jinja2 template into a Java Script function that you can call just like any other Java Script function and it will return a rendered string of your template and data. It is then up to you to do what you want with this string. In order to know what function to call pwt.jinja2js uses the name of the macro as the name of the Java Script function but also includes a special namespace node that you can include in the top of the Jinja2 template so that all the Java Script functions are called after their macro name prefixed with the namespace supplied. So that you can avoid namespace conflicts.

pwt.jinja2js can be invoked numerous ways, the two main ways been on the command line by calling the jinja2js script which you get for free after running easy_install pw.jinja2js, which just writes the output of templates disc which can then be served along with all your other static files. The other way is to configure a WSGI server to run the pwt.jinja2.wsgi.Resource application. This will find all your templates in the same way as Jinja2 does and will render them on the fly and return them with the correct mine type so your browser will treat it as Java Script. For performance considerations you should compress your Java Script including the generated Java Script from your templates with a tool like Closure compiler, YUI Compressor, or any other similar tools. I personally use pwt.recipe.closurebuilder to integrate the closure compiler with my zc.buildout build to get the best result.

 

Advertisements

Written by mkerrin

2011, April 1 at 3:55 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: