Once it became necessary to choose a template engine for use with Django. I didn’t want to dwell on one integrated solution, but I did a little research on the performance and usability of different products.
At the moment I chose Cheetah. That's why:
Performance
In search of a good template engine, I came across an article “
Multi engine template system ”, which compared the speed of drawing a template with different engines.
Testing was conducted on a fairly simple template, which contained:
- string variable ('hello world');
- traversing a list of 100 items;
- output a random number;
- Django model traversal with attribute output.

The clear leaders of the test - Cheetah and Mako. For this and other tests, Cheetah is more than 2 times faster than the Django template engine. Mako is 1.7..2 times faster than Django.
')
For Cheetah, I conducted my testing with a more complex pattern.
The base template contained three blocks: title, scripts, content. The page template inheriting these blocks is inherited from it. A string was placed in the title, in scripts - the result of traversing a list of 100 lines, in content - the result of traversing 100 objects with displaying five of their properties. The page using the template was called 100 times. The time of the first call was not counted.
As a result, the average page generation time was:
- Django - 437 ms;
- Cheetah - 211 ms.
Cheetah again twice as fast.
It would be interesting to see the results of testing Mako with a complex pattern (with inheritance and listing of objects).Syntax
I definitely made the choice between Mako and Cheetah because of the syntax. I really don't like the tag-like syntax of most template engines. Cheetah uses certain characters to indicate the beginning of a directive and a variable. By default, directives begin with the # character, and variables - with $. This behavior can be overridden using compiler directives, which I did:
# compiler-settings
directiveStartToken = ^
commentStartToken = #
#end compiler-settings
# print table row
^ def makerow (row, class)
<tr class = ”$ {class} _ggg”>
^ for $ cell in $ row:
<td> $ cell </ td> ^ slurp
^ end for
</ tr>
^ end def
Versatility
Cheetah and Python have a long history together (since 2001). Currently it is supported in all serious Python web frameworks, including Django, TurboGears, Pylons ...
It can also be used separately from any frameworks.
How to fasten?
There is a
django-cheetahtemplate project that makes it easy to switch to using Cheetah templates. By connecting django_cheetahtemplates, you get the
render_cheetah_response method, which you can use instead of render_to_response. You can also hook up to the
CheetahTemplate class, which behaves similarly to the Template class from Django.
Any problems?
Django parses chalons into nodes with a complex regular expression. Applying a template is reduced to executing the render () method for each node in the passed context. Cheetah assembles each template in a python class. This takes more time, but apparently gives a performance advantage when using this template. There is an
opinion that such an approach can be unsafe: a template can refer to some data that it is not supposed to know. With the inept use of templates, the logical separation of the document and its presentation can be violated.
I believe that if you need 100% universality and strict separation of data and presentation, it is better to use a bunch of XML-XSLT. However, in this case, a specially trained person will be needed to write the template, which is not always convenient. And the performance of such a solution is much less.Inheritance is possible only from the compiled template. Most often, this is not a problem, because the basic templates are written in the first place, and the compilation is done with a single command (
cheetah compile template_name
) and takes a couple of seconds. In addition, in the near future I will try to automate this operation or find a ready-made solution.
What to read
An important addition about Jinja2
The word Jinja is found in the comments 11 times, most often in a good context. Of course, I could not ignore such recommendations and put this template engine. Read about this in the
comments .