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
-
I hear a lot of people buzzing about Jinja and Genshi... do you have any specific arguments?vicvicvic– vicvicvic2011年02月15日 22:25:39 +00:00Commented Feb 15, 2011 at 22:25
-
@vicvicvic: I like Django/Jinja syntax because IMHO it is safer/easier for non-programmer UI designers. In Jinja you can pass arguments to a method, something I miss in Django.Paulo Scardine– Paulo Scardine2011年02月15日 22:49:11 +00:00Commented Feb 15, 2011 at 22:49
-
I had nice experience with Jinja2, surely is what you need.Tarantula– Tarantula2011年02月16日 01:14:09 +00:00Commented Feb 16, 2011 at 1:14
-
1I prefer using pure Python using a functional style.Keith– Keith2011年02月16日 02:32:18 +00:00Commented Feb 16, 2011 at 2:32
-
Running a bare-bones Django application isn't too much of a headache. You can be up and running with a basic static site in a very short space of time, and there's no need to set up a database in order to take advantage of the template syntax. Also super-easy to plug-in other template systems, or even write your own.Phil Powell– Phil Powell2011年02月23日 10:31:06 +00:00Commented Feb 23, 2011 at 10:31
1 Answer 1
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
Comments
Explore related questions
See similar questions with these tags.