[Zope] [ZGotW] Issue #1 Entry
Zope Guru of the Week
ZGotW@palladion.com
2000/01/20 17:18:29.4001 US/Pacific
===================================================================
Issue #1 -- DTML Subroutines
===================================================================
Status: Open
Zen Level: Novice (1/8)
Keywords: DTML
Submitted by: Tres Seaver tseaver@palladion.com
-------------------------------------------------------------------
Suppose you have been happily hacking away at your Zope site,
and discover that you are reusing a chunk of DTML over
and over::
<!-- Display all contained FooTypes in a table-->
<dtml-in "objectValues( 'FooType' )">
<tr>
<td> <a href="&dtml-id;"><dtml-var id></a> </td>
<!-- more FooType properties follow -->
</tr>
</dtml-in>
Now, you decide to "refactor" the site to follow the
"Once and Only Once":http://c2.com/cgi/wiki?OnceAndOnlyOnce dictum of
"Extreme Programming":http://www.extremeprogramming.org :
i.e., you'd like to make this snippet a reusable "subroutine,"
so that you could make changes to it in just one place.
How do you move this code out into a DTML Method and then
replace all the cloned copies with calls to that method?
-------------------------------------------------------------------
Entry from: ross lazarus rossl@med.usyd.edu.au
-------------------------------------------------------------------
making a dtml method showfoo and calling it as
<dtml-call "displayfootypes(_.None,_)"> will work. I've even done it once or twice. A long time ago.
In my view, the most efficient and effective way to deal with this common situation is to write a python script and make it an external method. Called in the same way.
My personal experience is that writing anything beyond mildly complex requires truly brain destroying dtml - the time to design, write, test, debug and so on is much longer with dtml once you go over a certain level of complexity compared with coding and testing in Python - which is much quicker for me. Remember, dtml was not designed for heavy duty programming. Python was.
I've also informally benchmarked python external methods to be 5 to 10 times faster on the first call (yes, there's overhead in dtml) for anything longer than a few lines - although zope caching means second and subsequent cached calls are generally indistinguishable.
Downside is that you have to move your external methods with the application. No big deal as long as you remember (!). You lose the total through the web thing too - editing python scripts means some additional software and futzing.
My 2c worth. Use dtml only for extremely simple stuff. When the going gets tough, make it easy on yourself and use external methods...
-------------------------------------------------------------------