比较 web.py 的模板程序和 Jinja2:优点和缺点
我正在向现有的软件添加一个简单的 Web 界面; web.py 非常适合这项工作,这就是我正在使用的。现在我正在研究使用什么模板引擎,并得出两个替代方案:使用 web.py 自己的 Templator 或使用 Jinja2。
我已经在应用程序中使用了这两个功能,并且我正在其中编写一些非常简单的模板来探索它们。我必须说我发现 Templator 更容易阅读,这可能是因为我是一名程序员而不是网页设计师(谁可能会发现 Jinja 更容易?)。
虽然我现在只生成(不合规;)丑陋的 HTML 页面,但我还将使用模板引擎来生成电子邮件和良好的旧纯文本文件。
对于任何实际目的来说,这两种软件都“足够快”,我想问一下广泛使用其中一种或两种软件的人,他们在易用性、代码整洁性、灵活性等方面的优点和缺点是什么。
I'm adding a simple web interface to an already existing piece of software; web.py fits the job just right and that's what I'm using. Now I'm researching what templating engine to use and came down to two alternatives: either using web.py's own Templator or using Jinja2.
I already have both working in the app and I'm writing some very simple templates in both to explore them. I must say I find Templator easier to read, that's probably due to me being a programmer and not a web designer (who would probably find Jinja easier?).
While I'm only generating (non compliant ;) ugly HTML pages now, I'll also use the templating engine to generate emails and good old plain text files.
Both softwares are "fast enough" for any practical purpose, I'd like to ask people who used extensively one or the other or both what are their strengths and weaknesses in the areas of ease of use, code cleanliness, flexibility, and so on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
快速浏览一下 Templator (我从未使用过)并将其与 Jinja2 (我已经广泛使用过)进行比较,我想说两者是非常相似...但 Templator 更接近到 Mako 而不是 Jinja。
Mako 和 Jinja 都支持:
而 Mako 和 Templator 都支持:
所有这三个支持:
Templator 支持一些奇怪的事情,我不相信 Jinja 或 Mako 会这样做:
并将它们编译为 Python 字节码,但它对所有内容都执行此操作,而不是将字符串传递到 Python 解释器以使用
safe_eval
。通过这样做,Jinja2 理论上可以免受模板级别上某些类型的攻击(但是当您从模板中收到恶意输入时,通常会遇到更大的问题)。至于其余部分,这很大程度上取决于您对语法的偏好。
Taking a quick look at Templator (which I have never used) and comparing it to Jinja2 (which I have used somewhat extensively), I'd say that the two are pretty similar ... but Templator is closer akin to Mako than to Jinja.
Mako and Jinja both support:
While Mako and Templator both support:
All three support:
Templator supports on weird thing that I don't believe either Jinja or Mako does:
Jinja takes the template code and compiles them to Python bytecode, but it does that for everything, rather than passing through strings to the Python interpreter to use
safe_eval
. By doing so Jinja2 is theoretically immune to certain types of attacks on the template level (But when you have hostile input from your templates you generally have a much bigger problem).As for the rest of it, it's all pretty much up to your preference for syntax.
在 Templetor 中对我来说困难的是模板继承。您必须在应用程序代码中选择一次基本模板,然后在实际模板中进行奇怪的属性设置,同时在基本模板中访问它,而不是 Jinja2 中存在的简单的块概念。如果您需要多个“大”块(例如页面主体),您仍然会遇到问题。
真正的块要优雅得多,而 Templetor 的“真正”Python 的灵活性并不是真正必要的,尽管它可能是不安全的。
What was hard for me in Templetor is template inheritance. Instead of simple concept of blocks which is present e.g. in Jinja2, you have to select the base template once in the app code, then do the weird attribute setting in the actual template while accessing it in the base template. Still you have problems if you need more than one “big” block like the page body.
Real blocks are much more elegant, and the flexibility of Templetor's “real” Python is not really necessary, while it probably can be unsafe.