比较 web.py 的模板程序和 Jinja2:优点和缺点

发布于 2024-12-02 00:28:37 字数 380 浏览 2 评论 0原文

我正在向现有的软件添加一个简单的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满天都是小星星 2024-12-09 00:28:37

快速浏览一下 Templator (我从未使用过)并将其与 Jinja2 (我已经广泛使用过)进行比较,我想说两者是非常相似...但 Templator 更接近到 Mako 而不是 Jinja。

Mako 和 Jinja 都支持:

  • 模板继承(您可以拥有所有页面都继承的布局)
  • 空白控制

而 Mako 和 Templator 都支持:

  • 在模板中嵌入“安全”Python。

所有这三个支持:

  • 添加到模板上下文(函数、对象、变量、作品)
  • 定义函数以封装模板中可重用的功能块(Jinja 称它们为“宏”。)
  • 条件和循环
  • 设置和获取局部变量。
  • 表达式求值
  • 缓存已编译的字节码以加快将来的执行速度。

Templator 支持一些奇怪的事情,我不相信 Jinja 或 Mako 会这样做:

  • 从模板代码中设置已编译模板对象的属性。 (坦率地说,实际上使用该功能似乎是错误的做法。模板可以确定需要根据上下文传入的内容设置的任何标志应该已经由 Jinja 获取模板代码

并将它们编译为 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:

  • Template Inheritance (You can have a layout that all your pages inherit from)
  • Whitespace control

While Mako and Templator both support:

  • Embedding "safe" Python in your templates.

All three support:

  • Adding to the template context (functions, objects, variables, the works)
  • Defining functions to encapsulate re-usable pieces of functionality in your templates (Jinja calls them "macros".)
  • Conditionals and loops
  • Setting and getting local variables.
  • Expression evaluation
  • Caching the compiled bytecode to speed up future execution.

Templator supports on weird thing that I don't believe either Jinja or Mako does:

  • Setting attributes on the compiled template object from within the template code. (Speaking frankly, actually making use of that feature seems like the wrong thing to do. Any flags that your template could determine need to be set from what has been passed in by the context should already be set by your application code.)

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.

终止放荡 2024-12-09 00:28:37

在 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文