Python 的模板系统或语言(主要是 HTML)
我正在寻找一个成熟的、易于使用的、强大的、独立的、“美丽的”Python 模板系统/语言。我主要对从 HTML 源生成(静态)HTML 感兴趣(因此 Markdown/RST/Textile 不相关)。
似乎有一系列的选择(Python wiki 有一个很长的列表),这使得选择相当令人畏惧。以下是我听说过或使用过的语言,按我个人的熟悉程度排名。
如果有兴趣,请随意将其制作成社区维基。
Django
优点:
- 如果您使用过 Django,那么语法会很熟悉且简单。
- Django 很棒的文档。
- 与逻辑有很大的分离。
- 积极支持和维护。
缺点:
- 并不是真正适合在独立模式下使用。如果您没有任何
INSTALLED_APPS
,我什至不知道加载模板标记库是否有效。 - 与整个 Django 项目的进度挂钩使得独立使用变得模糊。
- 也许过于非Pythonic 语法。
Jinja2
优点:
- 语法本质上是 Django++
- 可配置语法
- 维护良好
- 良好的文档
Genshi
- XHTML:ish 语法(好还是坏?)
- 因此锁定生成基于 XML 的输出?
- 可以直接在模板中使用 Python (
)
Mako< /a>
优点:
- 由 Pylons 支持,部署在 reddit.com 等网站上
缺点:
- 语法(快速浏览一下)让我觉得有点不均匀。
<%
、%
和$
?
我认为值得考虑的一些事情还有:
- Python 3 兼容性
- 编辑器支持(例如,是否有维护的 TextMate 捆绑包?)
我承认我对以下内容一无所知,除了他们有丑陋的网站。
猎豹
StringTemplate
I'm looking for a mature, easy-to-use, powerful, stand-alone, "beautiful" template system/language for Python. I'm primarily interested in generating (static) HTML from HTML sources (so Markdown/RST/Textile aren't relevant).
There seems to be an array of choices (the Python wiki has a very long list), which makes selecting quite daunting. The following are the languages I've heard of or used, ranked by my personal level of familiarity.
Feel free to make this into a community wiki, if there's interest.
Django
Pros:
- Familiar and easy syntax if you've used Django.
- Django's awesome documentation.
- Much separation from logic.
- Actively supported and maintained.
Cons:
- Not really made to be used in stand-alone mode. I don't even know if loading template tag libraries work if you don't have any
INSTALLED_APPS
. - Tied to the schedule of the entire Django project makes stand-alone usage fuzzy.
- Perhaps overly non-Pythonic syntax.
Jinja2
Pros:
- Syntax is essentially Django++
- Configurable syntax
- Well-maintained
- Good documentation
Genshi
- XHTML:ish syntax (good or bad?)
- Therefore locked into generating XML based output?
- Possible to use Python directly in templates (
<?python ... ?>
)
Mako
Pros:
- Backed by Pylons, deployed on sites like reddit.com
Cons:
- The syntax (from a quick glance) strikes me as a bit uneven.
<%
,%
, and$
?
Some things that I think are worth considering are also:
- Python 3 compatibility
- Editor support (Are there maintained TextMate bundles, for example?)
I admit I don't know anything about the following, except that they have ugly websites.
Cheetah
StringTemplate
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉,还没有代表发表评论,所以我将将此作为答案。
我只使用过 django 和 mako。似乎这两种模板语言之间的主要区别在于 Django 的设计就好像您不能真正信任模板设计者一样。您可以从他们如何限制您可以在模板中使用的代码中看到这一点,并且他们不允许在模板中使用 python 代码。 (提示关于 python 代码是否属于模板的争论)。对于我的项目,我既是程序员又是设计师,所以 Django 妨碍了我。
Mako 只是将模板解析为文本块和 Python 代码,并带有一些辅助函数。这意味着 Mako 的代码要小得多,并且学习起来似乎比 Django 快得多(假设您已经熟悉 python)。
例如:我能找到在 django 中分配变量的唯一方法是使用 with 块:(
请注意,business.employees.count 实际上是一个函数(business.employees.count()))。
而 Mako 中的等效代码是:
Django 大师:请随意纠正我。我的经验有限,但这就是我从 Django 切换到 Mako 的原因。
如果您只是想了解语法,这似乎是不同模板系统的相当不错的基础比较: http://catherinedevlin.pythoneers.com/cookoff.html
Sorry, don't have the rep yet to leave comments, so I'll leave this as an answer.
I've only used django and mako. Seems like the primary difference between these two template languages is that Django is designed as if you can't really trust the template designers. You can see this in how they limit the code you're allowed to use in templates, and they don't allow python code within a template. (Cue debate on whether python code belongs in a template or not). For my projects, I was both the programmer and designer, so Django got in my way.
Mako simply parses the template into blocks of text and python code, with some helper functions. This means that Mako's code is much much smaller, and it seems to be much faster to learn than Django, assuming you're already familiar with python.
For example: The only way I could find to assign a variable in django was using a with block:
(Note that business.employees.count is actually a function (business.employees.count())).
Whereas the equivalent code in Mako would be:
Django gurus: Feel free to correct me. I have limited experience, but this was why I switched off of Django to Mako.
This seems to be a pretty decent base comparison of the different templating systems, if you just want to get an idea of the syntax: http://catherinedevlin.pythoneers.com/cookoff.html