A summary of the pros and cons of the Python Web Development template engine _python

Source: Internet
Author: User
Tags html tags

Doing WEB development involves dealing with the template engine. I have also been exposed to a number of Python template engine, the feeling can be summed up.

First, according to my degree of familiarity to list:

Pytenjin: I used it when I was developing Doodle and 91 foreign teachers.
Tornado.template: I use it when I'm developing a daily newspaper.
Pyjade: I've been in the development of the daily newspaper.
Mako: I only used it in a small project that was premature.
JINJA2: I've only done some demo on it.

No other mention, such as the Django template, is said to be slow and difficult to use, I did not contact at all.

Second, again performance

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

So I still use the actual example to test it, finally chose the Doodle home page. It has several sub templates, several loops, several function calls and many variables, and has a certain representation. Given that the template engine outside Pytenjin does not support local caching, I removed the sidebar used to the cache, rendering only the main part.

The result of rendering 1000 times is: Pytenjin takes 0.65 seconds, takes 0.9 seconds after canceling preprocessing, and Tornado.template takes 1 seconds; JINJA2 takes 1.1 seconds.

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

The advantages of @pyTenjin are obvious, especially when it supports preprocessing. The primary function of this preprocessing is to compile some constants first, and render them without having to deal with them (because they have become strings); In addition, some functions can statically decide whether to open, and preprocessing can remove unwanted functional code (mainly if branch) in advance. In addition, you can cache the render results of arbitrary code snippets for a period of time without having to render them again.
@Jinja2 slower than tornado.template is something I didn't think of, as if it didn't match with a lot of tests.
@Mako are expected to be about the same as JINJA2. It can also cache the render results of code snippets.
@PyJade need to turn the Jade template into another template without caching, which is expected to be much slower.

Considering that in addition to Pyjade must not exist several times the performance gap, so pick a good can.

Third, the last to say ease of use

The advantage of @pyTenjin is that you can write arbitrary Python code.
The disadvantage is that the mark is more complex and unique, there are <?py ...? >;? PY ...? >, #{...}, #{{...}, {==...==}, {#==...==#}, ${...}, ${{...}, {#=...=#}, and {#==...==#} are all so diverse, but they look pretty cute.
Because of the use of < and > symbols, the editor is prevented from parsing when used inside HTML tags.
In addition, its tagattr () method is treated as True in the expr parameter 0 o'clock and needs to be modified by the source code, and it has no open source project to submit to pull request.
And it has only one developer, has not been updated for more than a year, the active degree is obviously not enough.

@Tornado. The advantage of template is that it's good with Tornado (after all, it's self-contained), functional and performance are OK.
The downside is that it's hard to locate errors when it's wrong, and it's a little bit less functional than other template engines (but I haven't encountered enough).
In addition, {% raw ...%} writes good trouble. None will appear as none in the output, rather than an empty string, causing the writing to be tiring.
Its output of HTML code is to turn the tail of the space, but the separate Python line of code appears as empty lines, it looks strange.

The advantage of @Jinja2 is that it is more functional, defines a number of auxiliary functions, has filter, and also has an inline if expression, which is more comfortable to write. In addition, it is able to adjust whitespace, which makes it output more beautiful HTML.
The disadvantage is that the cost of learning is high, the syntax is not pure python, and cannot even import the Python module and use the [item for item in list if item] Such a list parsing expression.
Another serious drawback is the inability to output non-ASCII strings, which must be used in a Unicode type, but it is cumbersome to ensure this.

@Mako has the advantage of being able to write arbitrary Python code as well as Pytenjin, as well as supporting filter like JINJA2 (actually accustomed to the function call).
The disadvantage is also high learning costs, syntax is more complex, the HTML editor is not friendly.

The advantage of @PyJade is that it's the fastest written (especially for the front end) and there's nothing to spare.
The downside is the same as JINJA2, which is that it has almost no documentation, and that the latest release version is not available and needs to be developed.

At present, I still continue to use Pytenjin forget, the other is either bad, or higher learning costs, and more out of the functional feeling is not necessary.

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.