开发者

Template system or language for Python (primarily HTML)

开发者 https://www.devze.com 2023-02-11 05:00 出处:网络
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/R

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


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:

{% with total=business.employees.count %}
    {{ total }} 
{% endwith %}

(Note that business.employees.count is actually a function (business.employees.count())).

Whereas the equivalent code in Mako would be:

<% total = business.employees.count() %>
${total}

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

0

精彩评论

暂无评论...
验证码 换一张
取 消