Summary of the advantages and disadvantages of the Python Web development template engine

  • 2020-04-02 13:38:56
  • OfStack

Doing Web development involves working with a templating engine. I've also been exposed to a number of Python templating engines, and I'm ready to summarize.

First, I will list them according to my familiarity:

PyTenjin: I used it when developing Doodle and 91.
Tornado. Template: I used it when developing zhihu daily.
PyJade: I had contact with it when I was developing zhihu daily.
Mako: I only used it on a small project that died early.
Jinja2: I've only done a few demos with it.

Not to mention others, such as the Django template, which is said to be slow and difficult to use, and I haven't touched it at all.

Second, performance

A lot of testing is just a big loop or something, very low-tech. In fact, the rendering time of the template is mainly spent on string processing, including stitching, coding, escaping, etc., while the loop measures the performance of Python runtime.

So I tested it with an actual example and chose the home page of Doodle. It has several subtemplates, several loops, a few function calls, and a number of variables that are representative. Considering that templating engines other than pyTenjin don't support local caching, I removed the sidebar from the cache and rendered only the main body.

The result of rendering 1000 times is: pyTenjin takes 0.65 seconds, and 0.9 seconds after pre-processing is cancelled; Tornado. Template takes 1.0 seconds; Jinja2 takes 1.1 seconds.

There are hundreds of lines of test code, 19 files, and I won't bother to list them. Other templating engines are too lazy to test.

The advantages of @pytenjin are obvious, especially as it supports preprocessing. The main purpose of this preprocessing is to compile some constants before rendering them (because they are already strings). In addition, some functions can be turned on or off statically, and preprocessing can remove unwanted function code (mainly if branches) in advance. In addition, it can cache the rendering results of any code segment without needing to rerender for a period of time.
@jinja2 is slower than Tornado. Template which I didn't think of and seems to be inconsistent with many tests.
@mako is expected to be similar to Jinja2. It can also cache the render results of code snippets.
PyJade needs to convert the Jade template to other templates and has no cache, which is expected to be much slower.

Considering that there is certainly no performance gap several times over PyJade, pick one that is easy to use.

Finally, ease of use

The advantage of @pytenjin is that you can write arbitrary Python code.
The disadvantage is that the markup is more complex and unique, there are < ? Py... ? > , < ? PY... ? > , # {... #}, {{... }}, {= =... = =}, {# = =... = = #}, ${... }, ${{... }}, {# =... = # #} and {= =... ==#} so many, but it looks pretty cute.
Due to the use of < and > Notation, used inside HTML tags, prevents the editor from parsing.
In addition, its tagattr() method is treated as True when expr parameter is 0, which needs to be fixed by changing the source code, and it has no open source project to submit a pull request.
And with only one developer, it hasn't been updated in over a year, so it's definitely not active enough.

The advantage of @tornado. Template is that it works well with Tornado (it comes with it, after all), and its functionality and performance are ok.
The downside is that it's hard to pinpoint what's wrong when something goes wrong, and it does have less functionality than other templating engines (though I haven't run into a shortage yet).
Also, {% raw... %} is a pain to write. None will appear as None in the output instead of an empty string, resulting in tiring writing.
The HTML code it outputs is stripped of headspace, but separate lines of Python code are displayed as blank lines, which can look odd.

The nice thing about @jinja2 is that it has a lot of functionality, it defines a lot of helper functions, it has filters, it has inline if expressions and it's a little bit more comfortable to write. In addition, it can adjust the whitespace, which makes the HTML it outputs look better.
The disadvantage is that the learning cost is high, the syntax is not pure Python, and you can't even import Python modules and use [item for item in list if item] to parse expressions.
Another serious drawback is the inability to output non-ascii strings, in which case unicode types must be used, but this is cumbersome to ensure.

The advantage of @mako is that it can write any Python code like pyTenjin and that it supports filters like Jinja2 (it's actually used to function calls).
The disadvantages are high learning costs, complex syntax, and unfriendly HTML editors.

The great thing about @pyjade is that it's fast to write (especially for the front end) and there's nothing extra.
The downside, like Jinja2, is that it has almost no documentation, and the latest release is not available, requiring development.

For now, I'm going to stick with pyTenjin. The rest is either bad or expensive to learn, and the extra functionality doesn't feel like it's necessary.


Related articles: