Chris Lamb

django-ctemplate

BlogProjectsAbout

View all my projects »

Compile Django templates to C.


CTemplate is a quick hack to demonstrate compiling Django templates to C via Cython. You can use a CTemplate object just as you would a regular Django Template object:

>>> t = CTemplate("Hello {{ name }}")
>>> c = Context({'name': 'Rebon'})
>>> t.render(c)
'Hello Rebon'

Behind the scenes, the Template object is compiled via Cython to a shared object (hopefully) providing a faster render method for that template. If CTemplate encounters a node that it cannot compile directly to C, it is pickled and executed "dynamically" at run-time.

Speedups of at least 5X-10X should be expected, assuming you do not have any uncompiled template nodes. In contrast, Jinja2 claims a 2X speed increase, although I am grossly misrepresentating the project here.

Performance comparisons are rather biased towards Django - in a typical Django project the Template object is constructed on each render, degrading the performance by at least one order of magnitude.

You can install django-ctemplate from PyPI:

$ pip install django-ctemplate
… or by adding django-ctemplate to your requirements.txt and re-running pip install -r requirements.txt.

You can browse the source tree, create or file bugs or download the code from Git:

$ git clone https://github.com/lamby/django-ctemplate

django-ctemplate is released under the MIT license.

View all my projects »