Michael Kerrin

Web, Python, JavaScript and related technologies

Archive for March 2011

pwt.jscompiler

with one comment

Following up on my pwt.recipe.closurebuilder announcement, I have pushed a new project to github called pwt.jscompiler. The goal of this project is to write a JavaScript compiler for Jinja2 template engine. My work builds on Jinja2 reusing as much as possible. The idea is to write templates that can be reused to a limited degree between your Python code running on the server and your JavaScript code running in the browser. There will be limits to what can be supported for now. This is a pre-alpha release. It needs a bit more work to be fully functional and with as few bugs as possible.

pwt.jscompiler will take a Jinja2 template with macros in it, and compile all the macros to JavaScript functions. I have added a namespace node to Jinja2 so that I can generate meaningful names on the macros and so that I can use Closure library namespace and dependency management features. Currently I don’t support not using closure library but this is a bug and will be fixed in the future.

Other features of the pwt.jscompiler is the ability to serve the templates via a WSGI application that compiles the template to JavaScript. This is handy for development where you can put <script> tags in your page pointing to the WSGI application and template name. It also supports integration with the pwt.recipe.closurebuilder. To use integration all you need to do is change the dependency recipe that finds and maps all your JavaScript dependencies from pwt.recipe.closurebuilder:dependency to pwt.jscompiler:dependency and include the directories of your Jinja2 templates like you would for any JavaScript dependencies. The integration will compile your templates to JavaScript into a temporary file and add this to your dependency tree getting compiled down along with the rest of your JavaScript dependencies.

Written by mkerrin

2011, March 3 at 3:11 pm