Python Web开发模板引擎优缺点总结
做 Web 开发少不了要与模板引擎打交道。我陆续也接触了 Python 的不少模板引擎,感觉可以总结一下了。
一、首先按照我的熟悉程度列一下:
pyTenjin:我在开发 Doodle 和 91 外教时使用。
Tornado.template:我在开发知乎日报时使用。
PyJade:我在开发知乎日报时接触过。
Mako:我只在一个早期就夭折了的小项目里用过。
Jinja2:我只拿它做过一些 demo。
其他就不提了,例如 Django 的模板,据说又慢又难用,我根本就没接触过。
二、再说性能
很多测试就是弄个大循环什么的,很没技术含量。其实模板的渲染时间主要消耗在字符串处理上,包括拼接、编码、转义等,而循环测的则是 Python runtime 的性能。
所以我还是用实际的例子来测试吧,最终选择了 Doodle 的首页。它有几个子模板、几个循环、几个函数调用和很多个变量,具有一定代表性。考虑到 pyTenjin 以外的模板引擎不支持局部缓存,我就把用到缓存的侧边栏去掉了,只渲染主体部分。
渲染 1000 次的结果为:pyTenjin 耗时 0.65 秒,取消预处理后耗时 0.9 秒;Tornado.template 耗时 1.0 秒;Jinja2 耗时 1.1 秒。
测试代码有几百行,19 个文件,我就懒得列出来了。其他模板引擎也懒得测了。
@pyTenjin 的优势很明显,特别是它支持预处理。这个预处理的主要作用是把一些常量先编译好,渲染时就不用再处理了(因为已经变成字符串了);此外,有些功能可以静态地决定是否开启,而预处理可以把那些不需要的功能代码(主要是 if 分支)提前去掉。此外还能缓存任意代码段的渲染结果,在一段时间内无需重新渲染。
@Jinja2 比 Tornado.template 慢是我没想到的,好像与很多测试不符。
@Mako 预计和 Jinja2 差不多。它也能缓存代码段的渲染结果。
@PyJade 需要把 Jade 模板转成其他模板,且无缓存,预计会慢很多。
考虑到除 PyJade 外肯定不存在几倍的性能差距,所以挑个好用的即可。
三、最后说易用性
@pyTenjin 的优点是可以写任意 Python 代码。
缺点是标记比较复杂和独特,有 、、#{...}、#{{...}}、{==...==}、{#==...==#}、${...}、${{...}}、{#=...=#} 和 {#==...==#} 这么多种,不过看上去还挺萌的。
由于使用了 符号,在 HTML 标签内部使用时,会阻碍编辑器进行语法解析。
另外,它的 tagattr() 方法在 expr 参数为 0 时当成了 True 来处理,需要改源码来修正,而它又没有开源项目可以提交 pull request。
而且它只有一个开发者,已经有一年多没更新了,活跃度明显不够。
@Tornado.template 的优点是与 Tornado 搭配还不错(毕竟是自带的),功能和性能都还行。
缺点是出错时很难定位到是哪写错了,而且与其他模板引擎相比,功能确实少了点(不过我还没遇到不够用的情况)。
另外,{% raw ... %} 写起来好麻烦。None 在输出时会显示成 None,而不是空字符串,导致写起来很累。
它输出的 HTML 代码是去掉头尾空格的,不过单独的 Python 代码行会显示成空行,看上去比较怪。
@Jinja2 的优点是功能多,定义了很多辅助函数,有 filter,也有内联的 if 表达式这种语法糖,写起来比较舒服。此外,它能够调整空白,这使得它输出的 HTML 比较好看。
缺点是学习成本较高,语法也不是纯 Python 了,甚至不能 import Python 模块和使用 [item for item in list if item] 这种列表解析表达式。
另一个严重的缺点是不能输出非 ASCII 的字符串, 遇到这种情况必须使用 unicode 类型,但要保证这点很麻烦。
@Mako 的优点是和 pyTenjin 一样可以写任意 Python 代码,又和 Jinja2 一样支持 filter(其实习惯了函数调用的话)。
缺点也是学习成本较高,语法比较复杂,对 HTML 编辑器不友好。
@PyJade 的优点是写起来最快(特别是对前端而言),没什么多余的东西。
缺点和 Jinja2 一样,更惨的是它几乎没有文档,而且最新的 release 版不可用,需要用开发版。
目前看来,我还是继续用 pyTenjin 算了,其他的要么不好用,要么学习成本比较高,而且多出来的功能感觉并不是非有不可的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

Golang and Python each have their own advantages: Golang is suitable for high performance and concurrent programming, while Python is suitable for data science and web development. Golang is known for its concurrency model and efficient performance, while Python is known for its concise syntax and rich library ecosystem.
