[mod_python] supporting modular mod_python extensions vs."folding" mod_psp

Geert Jansen geert at boskant.nl
Sat Jun 7 09:19:02 EST 2003


Dave,

> > <al-for iter="name" expr="keys">
> >     <tr>
> >          <td><al-value expr="name.value()"></td>
> >          <td><al-value expr="environ[name.value()]"></td>
> >     <tr>
> > </al-for>
> > 
> > This is not Python. Nor is it HTML.
> 
> That is true.  If you restrict yourself to Python and HTML 
> you are forced to do something like this:
> 
> for name in keys:
>     print '<tr>'
>     print '  <td>%s</td>' % name
>     print '  <td>%s</td>' % environ[name]
>     print '<tr>'
> 
> That does not look bad until you build a semi-complex application.
> 
> You find yourself in the situation where you cannot change 
> the HTML presentation without refactoring the surrounding 
> Python application logic.  Likewise you find it next to 
> impossible to change the application structure without 
> breaking the presentation.  The above approach leads to the 
> escalating difficulty and cost (in time and/or
> money) of enhancements.
>
> The introduction of a mechanism (or third "language") that 
> allows and/or imposes a clear interface between the 
> presentation and implementation increases the ability to 
> change one without major impact on the other.  This is a huge 
> win for applications that will change over time.

I don't agree with you that banning a 3rd party language implies that
you're stuck with the horrible

<%
	long and tedious python fragment here
%>

style of coding.

Draco tries to solve this problem in a different way, by still using
only Python as the scripting language. This works by dividing your code
in a functional part and a formatting part. The function part is put in
a so-called handler, which resides in a separate file (1 per directory).
The functional code execute the user request and puts all the results in
a dictionary dubbed the interface. This almost always reduces your
formatting code to something like this:
 
<%
    for tr in rows:
        print """
            html formatted string
             """ % tr
%>

The rows variable is set in the handler and contains completely
processed and ready to print data.

Using a special notation:

<%
    for tr in rows:
        print """%%>
            html formatted string
             <%%""" % tr
%>

you can even edit the html format string with your favourite
Dreamweaver/Frontpage/whatever.

The premise on which I've built Draco is that you can't elimiate all
code from the template, but you can minimize it. A possible solution is
to use a different templating language but IMHO this just adds
complexity because you now have two languages (plus html) instead of
one. Because Python is just as easy to learn as any old templating
language I don't see how this helps. Maybe at first Python code is a bit
scary but this should pass quickly.

Cheers,
Geert



More information about the Mod_python mailing list