templates

P

projecktzero

For some of the web programming I've done in Python, I've used
htmltmpl. I had some experience with it in Perl, and found a Python
version.

http://htmltmpl.sourceforge.net/

I like that there's nearly a complete separation between the
presentation and the code. This is great when one person is designing
the pages and another is writing the code to drive those pages.
However, that's usually not the case for me. I'm usually doing both,
and I'm thinking there's got to be something possibly better out there.
I'm curious about other templating systems. I wouldn't be opposed to a
little bit of code in the template. I just don't want to go to the
other extreme of something like PHP or ASP. i.e. all code in the
template.

What web templating systems do you use and why?

Mike
(Note: I posted this on the tutor mailing list week, but I didn't get
any replies, so I thought I'd try here.)
 
B

Bruno Desthuilliers

projecktzero a écrit :
For some of the web programming I've done in Python, I've used
htmltmpl. I had some experience with it in Perl, and found a Python
version.

http://htmltmpl.sourceforge.net/
>
I like that there's nearly a complete separation between the
presentation and the code. This is great when one person is designing
the pages and another is writing the code to drive those pages.
However, that's usually not the case for me. I'm usually doing both,
and I'm thinking there's got to be something possibly better out there.

May be... Depending on your definition of "better" !-)
I'm curious about other templating systems.

Then here are two starting points:
http://www.webwareforpython.org/Papers/Templates/
http://www.skreak.com/wp/
I wouldn't be opposed to a
little bit of code in the template. I just don't want to go to the
other extreme of something like PHP or ASP. i.e. all code in the
template.

You can do PHP without messing up logic and presentation.
What web templating systems do you use and why?

Mostly ZopePageTemplate, and actually playing with Myghty too.
 
C

Christoph Zwerschke

projecktzero said:
I like that there's nearly a complete separation between the
presentation and the code. This is great when one person is designing
the pages and another is writing the code to drive those pages.
...
What web templating systems do you use and why?

A plethora of such Python web templating systems exists.

If you really care about separation between code and layout, try
attribute languages like TAL or Kid.

TAL is used by Zope, but there is also a nice standalone implementation
called SimpleTAL.

Kid is used by TurboGears and can also be used with Webware for Python.
I think Kid is a very clever and innovative solution.

http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL
http://www.owlfish.com/software/simpleTAL/
http://kid.lesscode.org
http://www.turbogears.org
http://www.webwareforpython.org/KidKit/Docs/UsersGuide.html

-- Christoph
 
C

Christoph Zwerschke

projecktzero said:
For some of the web programming I've done in Python, I've used
htmltmpl. I had some experience with it in Perl, and found a Python
version.
What web templating systems do you use and why?

BTW, there are also a couple of other very clever concepts for creating
web pages from Python. It must not always be templating systems. E.g.

XIST: http://www.livinglogic.de/Python/xist/
Nevow: http://divmod.org/projects/nevow

-- Christoph
 
T

thakadu

I have used PyMeld (http://www.entrian.com/PyMeld/) which is one of
very few that gives a 100% separation of code and presentation, in fact
PyMeld is not strictly speaking a template system at all.
I have also used Cheetah. (http://www.cheetahtemplate.org/) However for
a recent project (http://muti.co.za) I ended up using Python's built in
%s templating engine. The reason for this was performance. The %s
native system is extremely fast. In my somewhat informal benchmarks
PyMeld was two orders of magnitude and Cheetah one order of magnitude
slower than Python native templates.
I don't mean to criticze these two systems, they are both excellent,
and I probably could have spent some time trying to figure out why they
were so much slower but in the end it was quicker for me to go with
something I knew was super fast. I did try compiling the Cheetah
templates which provided a small performace boost but it was still much
slower than native templates. (I also tried the new $ based native
template system in Python 2.4 but it doesnt have the flexible
formatting support that the % one has)
 
C

Christoph Zwerschke

thakadu said:
I have used PyMeld (http://www.entrian.com/PyMeld/) which is one of
very few that gives a 100% separation of code and presentation, in fact
PyMeld is not strictly speaking a template system at all.

Yes, it is more like XIST that I mentioned in another post.
The reason why this is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML. If you are only filling in a few words, then
native templates will be surely much more effective. But if you are
messing around with the structure of your page, inserting and adding
elements, then PyMeld will be probably a better way.

Here are two wiki pages listing template engines:

http://wiki.python.org/moin/WebProgramming
http://pythonwiki.pocoo.org/Template_Engines (German)

-- Christoph
 
P

Peter Hansen

Christoph said:
Yes, it is more like XIST that I mentioned in another post.
The reason why this is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML. If you are only filling in a few words, then
native templates will be surely much more effective. But if you are
messing around with the structure of your page, inserting and adding
elements, then PyMeld will be probably a better way.

Unless I'm misremembering, PyMeld is special amongst the "total
decoupling of code and presentation" crowd in that it does *not* convert
the whole page to objects in memory, but instead performs its operations
using regular-expression substitution, and serializing back to HTML is a
trivial operation since it's already serialized.

I'm sure the extra overhead versus simple % substitution is noticeable,
but I have to say I was surprised to hear that it was measured at two
orders of magnitude slower. I thought better performance (versus the
"manipulate in-memory as DOM" approach) was one of its advantages.

-Peter
 
R

Richie Hindle

[Christoph]
The reason why [PyMeld] is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML.
[Peter]
Unless I'm misremembering, PyMeld is special amongst the "total
decoupling of code and presentation" crowd in that it does *not* convert
the whole page to objects in memory, but instead performs its operations
using regular-expression substitution, and serializing back to HTML is a
trivial operation since it's already serialized.

Peter is right for PyMeld, Christoph is right for PyMeldLite (which is a
variant of PyMeld used by Spambayes, and only works with valid XHTML).

The performance of PyMeld therefore depends on how much work you make it do -
if you insert a couple of values into a page, it's very fast. If you do lots
and lots of operations on the whole of a large page, it can be very slow
because it's doing complex regular expression operations on a large string.
(It works this way because two of its design goals were to only touch the
parts of the page you ask it to touch, and to work with arbitrary, possibly
invalid, HTML.)

Incidentally, I'm changing PyMeld's license from Sleepycat to BSD, making it
free for use in closed source projects. (I'll get around to updating the
website RSN :cool:
 
H

has

Christoph said:
Yes, it is more like XIST that I mentioned in another post.
The reason why this is slower than native templates seems clear: You
convert the whole page to objects in memory, and then serialize
everything back to HTML.

PyMeld's not a good example to judge DOM-style templating by: the main
reason it's a poor performer is its implementation is very inefficient.
There's a couple of third-party redesigns kicking about the web if you
want to search for them; I've not tried them myself but I imagine
performance is one of the issues they address.

It is true that DOM-style templating engines will still tend to be a
bit slower at rendering than a macro-style templating engine even when
all else is equal, but that's a price you always pay for choosing a
dynamic over a static approach.

For example, I once did some quick-n-dirty comparisons for my own
HTMLTemplate engine [1] which is fairly well optimised pure Python
code, and reckon its performance on typical insertion and iteration
tasks is maybe 1/2 - 1/3 of Cheetah's. (IIRC, PyMeld ran up to 100x
slower in those particular tests due to its naive algorithms.)

OTOH, you have to remember that HTML rendering is only one small
component in the overall application, so you really have to consider
this against the performance of the rest of the application to know if
a 2-3x difference in rendering speed makes a significant difference or
not. Chances are the biggest bottlenecks will lie elsewhere. Besides,
if pedal-to-the-metal performance is your primary concern, you should
probably be writing everything in C++ anyway.

If you are only filling in a few words, then
native templates will be surely much more effective. But if you are
messing around with the structure of your page, inserting and adding
elements, then PyMeld will be probably a better way.

I dunno; DOM-style engines work fine even for simple jobs, while the
big macro-style engines can do a lot of fancy stuff too. I think the
major difference is that the DOM-style engines do things much more
simply. For example, HTMLTemplate's codebase, API and documentation are
maybe a tenth the size of Cheetah's. Rather than trying to provide
every possible feature users might ever want themselves (c.f.
Greenspun's Tenth Rule), they keep out of the way as much as possible
and let the original language provide for the user's needs.

HTH
 
T

thakadu

I would like to give a few more specifics about my "benchmarking". The
web page had about 10 simple fields and a table of roughly 30 table
rows. The method of generation the table rows was exactly the same as
the example in the PyMeld documentation ie you create the Meld, you
make a copy of a prototypr row instance, you generate your new rows,
which may have replaceable fields inside them, and finally you replace
the single prototype row with all the new rows you have generated. The
same test using the same data with Cheetah and native templates
resulted in (on an oldish 600Mhz box):
PyMeld: +-300ms
Cheetah: +-30ms
Native %s: +- 5ms
On newer hardware obviously the times will be very different but this
particular application has to be able to scale to very large tables.
I did not try PyMeldLite because the HTML I am using is exactlty that:
HTML and not XHTML. Again I am not criticising PyMeld, I love its
simplicity and clean api and the code is easily understandable so I
will probably take a look at it again at some time. Also its great that
it works with any snippet of HTML code it does not even have to be
valid HTML and I get a lot of invalid HTML from the designers.
Regarding Cheetah, I was using Cheetah 1.0. There is a 2.0 Release
Candidate out but I didnt want to be using anything in RC status for a
production site.
 
H

has

thakadu said:
I did not try PyMeldLite because the HTML I am using is exactlty that:
HTML and not XHTML.

FWIW, HTMLTemplate is pretty lax and not restricted to XHTML. The only
XML-ish requirement is that elements need to be properly closed if
they're to be used as template nodes, e.g. <p node="con:desc">...</p>
and <img node="con:foto" />, not <p node="con:desc">... and <img
node="con:foto">. Otherwise, it should cope with pretty much any old
markup as long as HTMLParser can make sense of it.
and I get a lot of invalid HTML from the designers

You might consider throwing HTMLTidy <http://tidy.sourceforge.net/> at
it.
 
T

thakadu

Yes I looked at that but I did not benchmark it. Basically it seems to
convert the Meld or part of a Meld into a %s template in any case and I
already knew that %s performace was very good. So if I had used PyMeld
combined with %s then sure it would be much faster but I wanted to
benchmark a pure PyMeld approach against a pure %s approach.

You are right thought that this could significantly speed things up if
one was prepared to use a dual approach (less clean but I would still
use it if it was convenient).
 
T

thakadu

I haven't got around to trying HTMLTemplate yet but it is on my list of
things to do. It would be great to see how it compares in perfomance
and simplicity to PyMeld and other DOM approaches.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top