简体   繁体   English

有没有理由我应该在我的Django应用中劝阻使用替代模板引擎?

[英]Is there a reason I should be dissuaded from using an alternative templating engine in my Django app?

I've done a few small-ish Django projects, and each time I've been struck by the apparent limitations of Django's templating language. 我做了一些小型Django项目,每次我都被Django模板语言的明显限制所震惊。 Just as a random example, I was shocked to learn that if, in the context of a template, I had a variable bar and a dict foo, I couldn't access foo[bar] unless I wrote my own filter to do it. 就像一个随机的例子,我很震惊地得知,如果在模板的上下文中,我有一个变量条和一个dict foo,我无法访问foo [bar],除非我编写了自己的过滤器来执行此操作。

I've read that the reason for this is because Django was created for environments where the people designing the pages were not programmers. 我读过这个的原因是因为Django是为那些设计页面的人不是程序员的环境创建的。 I understand that. 我明白那个。

But let's say that's not a problem for me. 但是,让我们说这对我来说不是问题。 Is there a reason why I should stick with Django's templating language, rather than switching over to something with a lot more power, like Mako (where you can even execute arbitrary Python expressions)? 有没有理由我应该坚持使用Django的模板语言,而不是切换到具有更强大功能的东西,比如Mako(你甚至可以执行任意Python表达式)?

I had the opportunity to use Mako for a school project a while back, and I really loved the power of it. 我有机会在一段时间内将Mako用于学校项目,我真的很喜欢它的力量。 For example, as part of the project, we had to make a big table, where building each row and cell was fairly complex. 例如,作为项目的一部分,我们必须制作一个大表,其中构建每一行和单元格相当复杂。 Yet, I could make my template look something like: 然而,我可以让我的模板看起来像:

<table>
    % for foo in foos:
        ${makerow(row)}
    % endfor
</table>

<%def name="makerow(row)">
    <tr>
        # Blah blah blah (possibly a call to makecell somewhere)
    </tr>
</%def>

Maybe this is a violation of separation of presentation and logic, but boy is it nice and clean. 也许这违反了表达和逻辑的分离,但是男孩是好的和干净的。 Subroutines! 子程序! Abstraction! 抽象! Good stuff. 好东西。

And a follow-up question: If using an alternative templating language isn't frowned upon by the Django community, does anyone have any to suggest? 还有一个后续问题:如果Django社区不赞同使用其他模板语言,那么有人有任何建议吗? Like I said, I really like Mako, but it's literally the only one I've used other than Django's. 就像我说的那样,我真的很喜欢Mako,但它确实是除了Django之外我唯一使用过的那个。

I'll be honest, I didn't thoroughly read the responses. 老实说,我没有仔细阅读回复。 But I'm guessing it's a lot of "no python in your templates" and "your view shouldn't have much logic" type stuff. 但我猜这是很多“模板中没有python”和“你的视图不应该有太多逻辑”类型的东西。

If you put idealism aside and opt for pragmatism then I think Mako is a fine choice. 如果你把理想主义放在一边并选择实用主义,那么我认为Mako是一个不错的选择。 I'm using it in a production capacity (mainly for speed, power and dynamic inheritance) for 3+ years now. 我现在已经将它用于生产能力(主要用于速度,功率和动态继承)3年以上。 It hasn't failed or been otherwise annoying in any way. 它没有以任何方式失败或以其他方式烦人。

The idealists are correct, but sometimes you have to go for what's doable vs what's right. 理想主义者是正确的,但有时候你必须去寻找可行的东西而不是正确的东西。 If you are not limited by the Django templating engine use it. 如果您不受Django模板引擎的限制,请使用它。 If you need more power, Mako and Jinja are fine choices. 如果你需要更多的力量,Mako和Jinja是不错的选择。

Django makes it very easy to swap out templating engines and keep most things working as before: http://docs.djangoproject.com/en/dev/ref/templates/api/#using-an-alternative-template-language Django可以很容易地换出模板引擎,让大多数事情像以前一样工作: http//docs.djangoproject.com/en/dev/ref/templates/api/#using-an-alternative-template-language

Executing arbitrary code in templates should not be considered an inherently good thing. 在模板中执行任意代码不应被视为本质上的好事。 Taking advantage of such functionality is usually a sign that your architecture is broken. 利用此类功能通常表明您的体系结构已损坏。

That said, if you read the Django documentation, it explicitly says that you should feel free to use, discard, and replace any components you wish. 也就是说,如果您阅读Django文档,它会明确表示您应该随意使用,丢弃和替换您希望的任何组件。 Django is intentionally modular, and in fact, the two most trivially-replaceable components are the templating engine and the ORM. Django是故意模块化的,事实上,两个最易于替换的组件是模板引擎和ORM。

If you want to use Mako instead of the Django templating engine, just use Mako. 如果你想使用Mako而不是Django模板引擎,只需使用Mako。

The one reason I'd refrain from using jinja, Mako or anything else is that it may not make your app future proof with django enhancements. 我不使用jinja,Mako或其他任何东西的一个原因是它可能不会使你的应用程序未来证明django增强功能。

There was a GSoc project proposal last year, by Alex Gaynor to make the template loading fast. 去年,Alex Gaynor提出了一个GSoc项目提案,以快速加载模板。 - It was then retracted in favor of NoSQL project. - 然后它被撤回以支持NoSQL项目。

But with many more core developers and faster clearing of tickets, I'd stick to django full stack, knowing fully well, that components have to be changed to by home grown ones eventually. 但是随着更多的核心开发人员和更快的票据清理,我会坚持django完全堆栈,完全清楚,组件必须最终由本土的组件改变。

If you are really looking for a glue framework on awesome python libraries, including the ones you choose, Flask is out there . 如果您真的在令人敬畏的python库上寻找胶水框架,包括您选择的那些,那么Flask就在那里

addons.mozilla.org is using Django + Jinga: https://github.com/jbalogh/zamboni addons.mozilla.org正在使用Django + Jinga: https//github.com/jbalogh/zamboni

Not sure whether or not the community frowns on Jinga, but many people like it, as an example. 不确定社区是否对Jinga皱眉,但很多人都喜欢它,作为一个例子。

If you did mean "app", rather than "project", and it's not for entirely private use, I would recommend that you not change the template engine; 如果你的意思是“应用”,而不是“项目”,而不是完全私人使用,我建议你不要改变模板引擎; it will make the app much less likely to ever be used by anyone else as it'll require them to alter some core settings, and it may break interaction between it and other apps or the project as a whole. 它会使应用程序更不可能被其他任何人使用,因为它需要他们改变一些核心设置,它可能会破坏它与其他应用程序或整个项目之间的交互。

Your example reminds me of the old PHP days when people would mix PHP with html all over the place. 你的例子让我想起了人们将PHP与html混合在一起的旧PHP时代。 Felt really powerful. 感觉真厉害。 Until one day people realized that the mess is unmaintainable. 直到有一天,人们意识到这个烂摊子是不可维护的。

If the design is chopped up into "functions", will a designer understand it then? 如果设计被切割成“功能”,那么设计师会理解它吗? It will probably annoy him. 它可能会惹恼他。

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

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