简体   繁体   English

何时在Python中使用模板引擎?

[英]When to use a Templating Engine in Python?

As a "newbie" to Python, and mainly having a background of writing scripts for automating system administration related tasks, I don't have a lot of sense for where to use certain tools. 作为Python的“新手”,主要有编写脚本以自动执行系统管理相关任务的背景,我对使用某些工具的位置没有多少意义。

But I am very interested in developing instincts on where to use specific tools/techniques. 但我对开发使用特定工具/技术的本能非常感兴趣。

I've seen a lot mentioned about templating engines, included zedshaw talking about using Jinja2 in Lamson 我已经看到很多关于模板引擎的提及,包括zedshaw谈论在Lamson中使用Jinja2

So I thought I would ask the community to provide me with some advice as to where/when I might know I should consider using a templating engine, such as Jinja2 , Genshi , Mako , and co. 所以我想我会请社区向我提供一些建议,告诉我何时/何时可能知道我应该考虑使用模板引擎,例如Jinja2GenshiMako和co。

As @mikem says, templates help generating whatever form of output you like, in the right conditions. 正如@mikem所说,模板有助于在适当的条件下生成您喜欢的任何形式的输出。 Essentially the first meaningful thing I ever wrote in Python was a templating system -- YAPTU , for Yet Another Python Templating Utility -- and that was 8+ years ago, before other good such systems existed... soon after I had the honor of having it enhanced by none less than Peter Norvig (see here ), and now it sports almost 2,000 hits on search engines;-). 基本上我在Python中编写的第一个有意义的东西是模板系统 - YAPTU ,用于又一个Python模板实用程序 - 这是8年多以前,在其他好的系统存在之前......我很荣幸让它在Peter Norvig(见这里 )上得到增强,现在它在搜索引擎上运行了近2000次点击;-)。

Today's templating engines are much better in many respects (though most are pretty specialized, especially to HTML output), but the fundamental idea remains -- why bother with a lot of print statements and hard-coded strings in Python code, when it's even easier to hve the strings out in editable template files? 今天的模板引擎在很多方面都要好得多(尽管大多数都非常专业,特别是对于HTML输出),但基本思想仍然存在 - 为什么在Python代码中使用大量的print语句和硬编码字符串呢?在可编辑的模板文件中输出字符串? If you ever want (eg) to have the ability to output in French, English, or Italian (that was YAPTU's original motivation during an intense weekend of hacking as I was getting acquainted with Python for the first time...!-), being able to just get your templates from the right directory (where the text is appropriately translated) will make everything SO much easier!!! 如果你想(例如)有能力输出法语,英语或意大利语(这是YAPTU在一个激烈的黑客周末期间的原始动机,因为我第一次熟悉Python ......! - ),能够从正确的目录(文本被适当翻译)获取模板将使一切变得更容易!

Essentially, I think a templating system is more likely than not to be a good idea, whenever you're outputting text-ish stuff. 从本质上讲,每当你输出文本内容时,我认为模板系统更可能不是一个好主意。 I've used YAPTU or adaptations thereof to smoothly switch between JSON, XML, and human-readable (HTML, actually) output, for example; 我已经使用YAPTU或其改编来平滑地在JSON,XML和人类可读(HTML,实际)输出之间切换,例如; a really good templating system, in this day and age, should in fact be able to transcend the "text-ish" limit and output in protobuf or other binary serialization format. 一个非常好的模板系统,在这个时代,实际上应该能够超越“text-ish”限制并以protobuf或其他二进制序列化格式输出。

Which templating system is best entirely depend on your specific situation -- in your shoes, I'd study (and experiment with) a few of them, anyway. 哪种模板系统最好完全取决于你的具体情况 - 无论如何,在你的鞋子里,我会研究(并试验)其中的一些。 Some are designed for cases in which UI designers (who can't program) should be editing them, others for programmer use only; 有些设计用于UI设计者(无法编程)应该编辑它们的情况,其他用于程序员使用的情况; many are specialized to HTML, some to XML, others more general; 许多专门针对HTML,一些针对XML,另一些针对XML; etc, etc. But SOME one of them (or your own Yet Another one!-) is sure to be better than a bunch of print s!-) 等等,但其中一个(或你自己的又一个! - )肯定比一堆print更好! - )

A templating engine works on templates to programmatically generate artifacts. 模板引擎在模板上工作,以编程方式生成工件。 A template specifies the skeleton of the artifact and the program fills in the blanks. 模板指定工件的骨架,程序填充空白。 This can be used to generate almost anything, be it HTML, some kind of script (ie: an RPM SPEC file), Python code which can be executed later, etc. 这可以用来生成几乎任何东西,无论是HTML,某种脚本(即:RPM SPEC文件),可以在以后执行的Python代码等。

Simply put, templating lets you easily write a human readable document by hand and add very simple markup to identify areas that should be replaced by variables, area that should repeat, etc. 简单地说,模板化可让您轻松手动编写人类可读文档,并添加非常简单的标记,以识别应由变量替换的区域,应重复的区域等。

Typically a templating language can do only basic "template logic", meaning just enough logic to affect the layout of the document, but not enough to affect the data that's fed to the template to populate the document. 通常,模板语言只能执行基本的“模板逻辑”,这意味着只有足够的逻辑来影响文档的布局,但不足以影响提供给模板以填充文档的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM