Python Web Development template Engine pros and cons summary

Source: Internet
Author: User
Web Development is about working with the template engine. I have also come into contact with a lot of Python template engine, I feel can be summed up.

First, according to my familiarity with the level of the list:

Pytenjin: I used to develop Doodle and 91 foreign teachers.
Tornado.template: I use it when I develop a daily newspaper.
Pyjade: I've been in touch with the Daily news of the development.
Mako: I used it only in a small project that was aborted in the early days.
JINJA2: I've only done some demo on it.

Other than that, such as the Django template, it is said to be slow and difficult to use, I have never touched.

Second, say the performance

A lot of testing is to get a big loop or something, very technical content. In fact, the rendering time of the template is mainly consumed in string processing, including splicing, encoding, escaping, and so on, while the cyclic measurement is the performance of Python runtime.

So I still use the actual example to test it, and finally chose the first page of Doodle. It has several sub-templates, several loops, several function calls, and a number of variables, which have a certain representation. Given that the template engine outside of Pytenjin does not support local caching, I have removed the side bar of the cache, rendering only the body part.

The result of rendering 1000 times is: Pytenjin takes 0.65 seconds, takes 0.9 seconds to cancel preprocessing, Tornado.template takes 1 seconds, JINJA2 takes 1.1 seconds.

The test code has hundreds of lines, 19 files, I do not bother to list out. Other template engines are too lazy to test.

The advantage of @pyTenjin is obvious, especially when it supports preprocessing. The main function of this preprocessing is to compile some constants first, and no more processing when rendering (because it has become a string); In addition, some functions can statically decide whether to open, and preprocessing can remove the unwanted function code (mainly the If branch) prematurely. You can also cache rendering results for any code snippet without having to re-render it for a period of time.
@Jinja2 slower than tornado.template is something I didn't expect, as if it didn't match a lot of tests.
The @Mako is expected to be similar to JINJA2. It can also cache the rendering results of code snippets.
@PyJade need to convert the Jade template to another template without caching, which is expected to be much slower.

Considering that except Pyjade, there is certainly not a few times the performance gap, so pick a useful.

Third, the final say ease of use

The advantage of @pyTenjin is that you can write any Python code.
The disadvantage is that the tags are more complex and unique, there are, #{...}, #{{...}, {==...==}, {#==...==#}, ${...}, ${{...}}, {#=...=#} and {#==...==#} so many, But it looks pretty cute.
The use of < and > symbols prevents the editor from parsing syntax when used inside an HTML tag.
In addition, its tagattr () method is treated as True for the expr parameter 0 o'clock, which needs to be modified by the source code, and it does not have an open source project to submit pull request.
And it has only one developer, has not been updated for more than a year, the activity is obviously not enough.

The advantage of @Tornado. Template is that it's good with Tornado (after all), and the functionality and performance are fine.
The downside is that it's hard to pinpoint where it went wrong, and it's really a little bit less than the other template engines (but I haven't had enough).
In addition, {% raw ...%} is very troublesome to write. None will show up as none at the output, not an empty string, resulting in a very tiring writing.
It prints the HTML code to turn around the trailing blanks, but the individual Python code lines appear to be a blank line, which looks odd.

The advantages of @Jinja2 are many functions, defined a lot of auxiliary functions, there is a filter, there is an inline if expression of this syntax sugar, write more comfortable. In addition, it is able to adjust the white space, which makes it more beautiful to output HTML.
The disadvantage is that the learning cost is higher, the syntax is not pure python, and even cannot import the Python module and use the [item for item in list if item] Such a list to parse the expression.
Another serious drawback is that you cannot output non-ASCII strings, and you must use Unicode types in this case, but make sure that this is a hassle.

The advantage of @Mako is that as with Pytenjin, you can write any Python code and support filter like JINJA2 (which is actually used to function calls).
The disadvantage is the higher the cost of learning, the syntax is more complex, the HTML editor is not friendly.

The advantage of @PyJade is that it's the quickest to write (especially for the front end), and there's nothing extra.
The disadvantage is that, like JINJA2, it has almost no documentation, and the latest release version is not available and needs to be developed.

At present, I still continue to use Pytenjin, the other is either not good use, or learning cost is higher, and more out of the functional feel is not necessary.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.