[Zope] [ZGotW] Issue #1 (Closed)
Zope Guru of the Week
ZGotW@palladion.com
Fri, 28 Jan 2000 10:12:30 US/Pacific
The current issue of "Zope Guru of the Week" has closed:
===================================================================
Issue #1 -- DTML Subroutines
===================================================================
Status: Closed
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.
The intent is to be able 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,
it might be '[ "Folder", "DTML Document" ]' [1].
How do you move this code out into a DTML Method and then
replace all the cloned copies with calls to that method?
[1] *Please pardon my inverting the single/double quotes here:
while not valid DTML, it **is** valid Python, and renders
correctly as StructeredText.*
-------------------------------------------------------------------
Reviewed by: Tres Seaver tseaver@palladion.com
-------------------------------------------------------------------
Summary
In spite of having to answer the question before I made the
point less than muddy, Ross Lazarus righfully pointed out that,
for complicated logic, DTML Methods (the "subroutines" of the
question) have distinct disadvantages:
* convoluted syntax, because DTML isn't really designed for
such cases
* a small performance hit (compared to External Methods)
Jeff Hoffman answered the orignal intent of the question (as
revised after feedback from Ross), showing the correct syntax
for passing parameters to a DTML Method using keyword
arguments::
<dtml-var "table_of_type(this(), _, type_list = ['FooType'])">
This syntax allows 'table_of_type' to iterate over 'type_list'
as passed in by the caller::
<dtml-in "objectValues(type_list)">
<li><a href="&dtml-id;">&dtml-id;</a>
</dtml-in>
Evan Simpson amended Jeff's example for calling such methods
as acquired from elsewhere in the tree without having to munge
the namespace stack using 'dtml-with' (in his example, calling
a method 'table_of_type' acquired from 'lib'::
<dtml-var "lib.table_of_type(target, _, type_list='Folder')">
Congratulations and kudos to all three for their answers!
-------------------------------------------------------------------