简体   繁体   English

我应该使用Mako进行模板制作吗?

[英]Should I use Mako for Templating?

I've been considering a templating solution, although my choices are between Mako and Genshi. 我一直在考虑一种模板解决方案,尽管我的选择是在Mako和Genshi之间进行。 I find templating in Genshi a bit ugly, so I'm shifting more towards Mako. 我发现在Genshi中制作模板有点丑陋,所以我将更多地转向Mako。

I've gone to wonder: what is so good about the fact that Mako allows embedded Python code? 我想知道:Mako允许嵌入Python代码这一事实有什么好处? How is it convenient for the average joe? 一般的乔如何方便?

Wouldn't templating JUST suffice without having embedded Python code? 如果没有嵌入式Python代码,就无法满足需求吗?

As the mako homepage points out, Mako's advantages are pretty clear: insanely fast, instantly familiar to anyone who's handy with Python in terms of both syntax and features. 正如mako 主页所指出的那样,Mako的优势非常明显:速度很快,对于在语法和功能方面都非常熟悉Python的任何人都立即熟悉。

Genshi chooses "interpretation" instead of ahead-of-time Python code generation (according to their FAQ , that's for clarity of error messages) and an "arm's length" approach to Python (eg by using xpath for selectors, xinclude instead of inheritance, etc) so it might be more natural to people who know no Python but are very competent with XML. Genshi选择了“解释”,而不是提前生成Python代码(根据其FAQ ,这是为了清除错误消息),并选择了“独断”的Python方法(例如,通过使用xpath作为选择器,使用xinclude代替继承,等),这对于不懂Python但精通XML的人来说可能更自然。

So what's your "audience"? 那么,您的“受众群体”是什么? If Python programmers, I suggest Mako (for speed and familiarity); 如果是Python程序员,我建议使用Mako(以提高速度和熟悉度); if XML experts that are uncomfortable with Python, Genshi might be a better fit (for "arm's length from Python" approach and closer match to XML culture). 如果XML专家不熟悉Python,那么Genshi可能更合适(因为“与Python距离很远”的方法和与XML文化的更紧密匹配)。

You mention "the average Joe", but Joe doesn't know Python AND xpath is a deep dark mystery to him; 您提到了“普通的Joe”,但是Joe不了解Python,而xpath对他来说是一个深深的奥秘。 if THAT was really your audience, other templating systems such as Django's might actually be a better fit (help him to avoid getting in trouble;-). 如果真的是您的听众,那么其他模板化系统(例如Django的系统)实际上可能更合适(帮助他避免麻烦;-)。

Wouldn't templating JUST suffice without having embedded Python code? 如果没有嵌入式Python代码,就无法满足需求吗?

Only if your templating language has enough logical functionality that it is essentially a scripting language in itself. 仅当您的模板语言具有足够的逻辑功能时,它本身实际上就是一种脚本语言。 At which point, you might just as well have used Python. 到那时,您可能也已经使用过Python。

More involved sites often need complex presentation logic and non-trivial templated structures like sections repeated in different places/pages and recursive trees. 涉及更多的站点通常需要复杂的表示逻辑和非平凡的模板结构,例如在不同位置/页面和递归树中重复的部分。 This is no fun if your templating language ties your hands behind your back because it takes the religious position that "code in template are BAD". 如果您的模板语言将您的双手紧紧地绑在了背后,这将很无聊,因为它的宗教立场是“模板中的代码是错误的”。

Then you just end up writing presentation helper functions in your Python business logic, which is a worse blending of presentation and application logic than you had to start with. 然后,您最终只是在Python业务逻辑中编写了演示文稿帮助程序函数,这比起初要糟糕得多,混合了演示文稿和应用程序逻辑。 Languages that take power away from you because they don't trust you to use it tastefully are lame. 那些因为不信任您高雅地使用它而剥夺您力量的语言是la脚。

This seems to be a bit of a religious issue. 这似乎是一个宗教问题。 Django templates take a hard line: no code in templates. Django模板要求严格:模板中没有代码。 They do this because of their history as a system used in shops where there's a clear separation between those who write code and those who create pages. 他们之所以这样做,是因为其作为商店中使用的系统的历史,在商店中编写代码的人与创建页面的人之间存在明显的区别。 Others (perhaps you) don't make such a clear distinction, and would feel more comfortable having a more flexible line between layout and logic. 其他人(也许是您)没有做出如此明确的区分,并且在布局和逻辑之间具有更灵活的界线会感到更加自在。

It really comes down to a matter of taste. 这真的归结为口味问题。

Genshi is conceived (read: biased, optimized) for generation of xml docs (even if it does offer support for generating any kind of text document). Genshi是为生成xml文档而设想的(阅读:有偏见的,经过优化)(即使它确实支持生成任何类型的文本文档)。 Mako and Django templates are conceived as generic text templating system. Mako和Django模板被认为是通用的文本模板系统。 Evoque also, but with one fundamental difference that it makes the design choice to only allow python expressions in templates ie no python statements . Evoque也有,但有一个根本的区别,那就是设计选择只允许在模板中使用python 表达式 ,即没有python 语句

One important net result of this is that Evoque is able to execute template evaluation in a sandbox -- ie you could safely give untrusted users write-access to a template's source code -- a feature that is virtually impossible for template engines that also allow embedding of python statements . 一个重要的净结果是Evoque能够在沙箱中执行模板评估-即,您可以安全地给不受信任的用户提供对模板源代码的写访问权限-该功能对于模板引擎几乎是不可能的,该功能还允许嵌入的python语句 Oh, and while not losing out any in a direct feature comparison, Evoque is in some cases actually faster than Mako, and it also runs on Python 3. 哦,虽然在直接的功能比较中并没有丢失任何东西,但Evoque在某些情况下实际上比Mako快,并且它也可以在Python 3上运行。

You could discipline yourself to not inject any Python code within the template unless it's really the last resort to get the job done. 您可以训练自己不要在模板内注入任何Python代码,除非它确实是完成工作的最后手段。 I have faced a similar issue with Django's template where I have to do some serious CSS gymnastics to display my content. 我在Django的模板中遇到了类似的问题,我必须做一些认真的CSS体操来显示我的内容。 If I could have used some Python code in the template, it would have been better. 如果我可以在模板中使用一些Python代码,那就更好了。

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

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