[mod_python] Servlets and templates and SimpleTAL (oh my!)

Daniel J. Popowich dpopowich at mtrsd.k12.ma.us
Fri Jun 25 11:57:06 EDT 2004


Eric Walstad writes:
> ... Daniel Popowich, in the Servlets tutorial, advocates using
> HyperText (I think), which seems to embed the presentation pretty
> firmly into the logic of the web app.  I like keeping the
> presentation separate from the logic as much as possible.  

I quote the tutorial: "True confessions: I hate writing HTML. So, I
have always used packages like HTMLgen or HyperText."

To be clear, I'm advocating using packages like HyperText over writing
HTML strings, NOT embedding presentation into logic.  In fact, I make
rather strong statements in the README that comes with servlets
advocating the separation of code and content.

For tutorial purposes, it was too difficult to do anything other than
include the content in the servlet files, however, you will notice
that the content is always seperated from the logic and, typically, is
stored as a string at the end of the file.  This string could just as
easily have come from a database or template files.

Also, my view of HTML, and by extension, HyperText, is as a GUI widget
set.  DIVs and SPANs become containers whereby you can apply text
style and other display characteristics.  This is not too unlike a box
widget in tkinter or wxpython: create a widget, apply attributes,
stuff content into it.  In general, I don't think of HyperText objects
that manage page layout or forms (INPUTs generating buttons,
checkboxes, etc.) as content, but as logic!  Certainly, if you were
writing a tkinter app, that would be the case.

> I'm curious to know if anyone has used SimpleTAL with Servlets or
> can offer insight on this mixture to someone with no Python Servlet
> experience.

Anyway, as to your question about using a templating tool with
Servlets.  Why not?  You can store your content anywhere and using,
perhaps, PATHINFO as a lookup key, retrieve your content from a
database or directory hierarchy which is removed from your application
files. 

As an example, I have used servlets to write a fairly comprehensive
content management system: users (teachers/staff of a public school
system) with little or no experience with HTML and administrators with
even less experience maintaining a web server can log in to my web app
and maintain the website completely on-line.  It maintains a common
look & feel across a district of seven schools plus the
superintendent's office.  The logic is in a half-dozen or so python
files and the content is stored in a directory hierarchy outside of my
documentroot.  I use path_info (see Servlet doc) as a relative path
from the school's content root directory.  The files have meta
information stored with them (not unlike templates) which is applied
to the content and then returned via my app.

I should perhaps add a templating example to the tutorial; it would be
something like this: a file would exist (exmple.tpl) with the content
of the BODY and have tags or some such which would be replaced with
information collected from a form.  The servlet (template.mps) would,
on a GET, produce a form (HyperText widgets) and on a POST would read
the example.tpl, apply the data collected from the POST and return the
processed template as the content.

Hope this helps.

Cheers,

Daniel Popowich






More information about the Mod_python mailing list