[ZGotW] Issue #1 Entry
=================================================================== 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... -------------------------------------------------------------------
Zope Guru of the Week wrote:
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.
I'm rather confused by this question. Why not simply put the DTML you want to factor out in a seperate DTML method called 'displayfootypes' (or whatever), and then use <dmtl-var displayfootypes> whenever you want to use it? Regards, Martijn
Martijn Faassen wrote:
Zope Guru of the Week wrote:
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.
I'm rather confused by this question. Why not simply put the DTML you want to factor out in a seperate DTML method called 'displayfootypes' (or whatever), and then use <dmtl-var displayfootypes> whenever you want to use it?
Umm, sorry, I guess that my intent isn't clear. The intent is to reuse the loop, but be able to vary the meta_type(s) being passed to the loop: in one spot, it would be 'FooType', but in another, "[ 'Folder', 'DTML Document' ). Thanks for pointing this out -- I'll modify the question accordingly. -- ========================================================= Tres Seaver tseaver@palladion.com 713-523-6582 Palladion Software http://www.palladion.com
participants (3)
-
Martijn Faassen -
Tres Seaver -
Zope Guru of the Week