For Web development, you must deal with the template engine. I have been familiar with many Python template engines one after another, so I can summarize them.
I. First, follow my familiarity to list:
PyTenjin: I used it when developing dota2 and 91 foreign teachers.
Tornado. template: used when developing zhihu daily report.
PyJade: I used it when I was developing zhihu daily.
Mako: I used it only in a small project that was aborted in the early stages.
Jinja2: I only used it for some demos.
Nothing else. For example, the Django template is said to be slow and difficult to use, and I have never touched it.
Ii. Performance
A lot of tests are about making a big loop or something, and there is no technical content. In fact, the rendering time of the template is mainly consumed in string processing, including splicing, encoding, escape, and so on, while the cyclic testing is the performance of Python runtime.
So I used the actual example to test it and finally chose the DoCoMo homepage. It has several sub-templates, several loops, several function calls, and many variables, which are representative. Considering that the template engine other than pyTenjin does not support partial caching, I removed the sidebar used for caching and only rendered the main part.
The result of rendering 1000 times is: pyTenjin took 0.65 seconds, and 0.9 seconds after preprocessing is canceled; Tornado. template took 1.0 seconds; Jinja2 took 1.1 seconds.
The test code contains hundreds of lines and 19 files, so I am too lazy to list them. Other template engines are too lazy to test.
@ PyTenjin has obvious advantages, especially its support for preprocessing. The main function of this preprocessing is to compile some constants first, so that you do not need to process them during rendering (because they have already become strings). In addition, some functions can be used to determine whether to enable them statically, preprocessing removes unnecessary functional code (mainly if Branch) in advance. In addition, the rendering results of any code segment can be cached without re-rendering for a period of time.
@ Jinja2 is slower than Tornado. template, which seems to be inconsistent with many tests.
@ Mako is expected to be similar to Jinja2. It can also cache rendering results of code segments.
@ PyJade you need to convert the Jade template to another template without caching, which is expected to be much slower.
Considering that there must be no performance gap several times except for PyJade, you can select a useful one.
Iii. Usability
@ PyTenjin has the advantage of being able to write any Python code.
The disadvantage is that tags are complex and unique. <? Py...?> , <? PY...?> ,#{...} ,#{{...}}, {=... ==}, {#==... ==#}, $ {...}, $ {{...}}, {# =... ##}And {#==... ==#} there are so many, but it looks pretty cute.
When using the <and> symbol inside the HTML Tag, the editor is blocked from parsing the syntax.
In addition, its tagattr () method is treated as True when the expr parameter is 0. You need to modify the source code to correct it, and it does not have any open-source project to submit a pull request.
In addition, it only has one developer who has not been updated for more than a year and is obviously not active enough.
@ Tornado. The advantage of template is that it works well with Tornado (after all, it comes with it), and its functions and performance are also good.
The disadvantage is that when an error occurs, it is difficult to locate the cause of the error, and the function is indeed a little less than that of other template engines (but I have not encountered the problem of insufficient use ).
In addition, it is troublesome to write {% raw... %. None is displayed as None in the output, rather than a null string, which causes fatigue in writing.
The HTML code output by this tool removes leading and trailing spaces. However, a separate Python code line is displayed as a blank line, which looks strange.
@ Jinja2 has many features and defines many auxiliary functions, including filter and inline if expressions, which are quite comfortable to write. In addition, it can adjust the white space, which makes the HTML output better.
The disadvantage is that the learning cost is high, and the syntax is not pure Python. You cannot even import the Python module or use the list parsing expression [item for item in list if item.
Another serious disadvantage is that non-ASCII strings cannot be output. In this case, the unicode type must be used, but it is very troublesome to ensure this.
@ Mako the advantage is that it can write arbitrary Python code like pyTenjin, and support filter like Jinja2 (in fact, used to function calls ).
The disadvantage is that the learning cost is high, the syntax is complicated, and the HTML editor is unfriendly.
@ PyJade the advantage is that it is the fastest to write (especially for the front-end) and there is nothing redundant.
The disadvantage is that Jinja2 has almost no documentation, and the latest release version is unavailable and must be developed.
At present, it seems that I continue to use pyTenjin, and the other ones are either not easy to use, or the learning cost is relatively high, and the extra features are not indispensable.