[ANN] HTMLTemplate 1.0.0

Graham Dumpleton grahamd at dscpl.com.au
Thu Jun 3 23:30:16 EDT 2004


Peter Maas <peter.maas at mplusr.de> wrote in message news:<c9k8gt$sqh$1 at swifty.westend.com>...
> has schrieb:
> > Announcing the final v1 release of HTMLTemplate; best damn HTML
> > templating system evar, or yer money back. Enjoy! :)
> 
> Good work BUT
> there are some other Python templating frameworks around, e.g.
> 
> - ....... etc
> 
> Did you find these alternatives unsatisfactory? If somebody wants
> to use a Python templating framework why should he prefer
> HTMLTemplate?

Having had a play with HTMLTemplate, albeit after a few tweaks, one area
where it seems to be perhaps better suited than other methods is when
an event driven system is being used.

This is because with most of the other systems the filling out of data and
rendering of the template is an atomic operation. In other words, you
execute the operation to render the template and it calls out of the render
engine to collect data which is used to fill in the data.

This means the data must already exist if you want to avoid a potentially
blocking operation which collects it. In an event driven system this isn't
good as it means nothing else in the application can run. The only way
around it is to initiate the rendering inside the context of a separate thread.

When you have to do this on top of an event system it can get messy
though as you need to then deal with communication between the thread
and the event system when the rendering is done to say that the event
system can take up from where it left off.

Now in order to avoid use of threads, one could go to the extent of collecting
all the data first, making use of the event system appropriately to ensure
no blocking operations occur, but that means caching all the data which
goes towards rendering the template up till the point it needs to be rendered.

The idea that HTMLTemplate uses of constructing a DOM type model of the
page is therefore convenient as you can construct the template at some
point and then over time as event driven callbacks are triggered indicating
return of required data, you can fill out the template in bits.

Only when the last bit of data has finally arrived do you then render the
template back into a textual form. If at any point one of the event callbacks
indicates an error you can just abandon the template and send back an
error along with the output of some of template page instead.

Having said that, the only problem with HTMLTemplate is that its render()
function actually enforces the model which I said is no good for an event
driven system. Ie., data collection is triggered by the action to render the
template.

To get around this, the render() function implementation needs to be split
into three phases, the cloning of the template, the execution of the callback
to fill in the data and the rendering of the template back to text. If the
cloning and rendering are in separate functions, it then allows an event
driven system to control things in a more appropriate manner.

Namely, in one event system callback it can clone the template, in a series
of other event system callbacks it can fill out the data and then finally in a
last event system callback it can render the template to text and deliver
the result to the HTTP servlet engine or whatever other mechanism is used
to serve up the result.

The changes to the code aren't that hard and it would make it that much
more flexible and useful in event driven systems. I have forwarded the
appropriate changes direct to the author and hopefully he will include
them, otherwise I will have to use a derived class which delves into the
private bits of their code. :-(

Now as to other template systems that are event driven system friendly,
the only one I know of for Python is Nevow, but that is bound up with
Twisted and thus couldn't be used with any other system. For my mind,
from the little I have looked at the Nevow examples, I am also not keen
on its magic system for callbacks dependent on naming. This sorts of
binds it quite tightly to a particular way of building your classes which I
would rather avoid.

Overall therefore, for what I am doing HTMLTemplate looks like a good
option.



More information about the Python-list mailing list