[Zope] [ZGotW] Issue #1 Entry

Zope Guru of the Week ZGotW@palladion.com
2000/01/21 07:20:43.541 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.
 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.*
-------------------------------------------------------------------

   Entry from:  Jeff K. Hoffman jeff@goingv.com
-------------------------------------------------------------------
First, create a DTML Method:

<pre>
  <dtml-in "objectValues(type_list)">
  <tr>
    <td><a href="&dtml-id;">&dtml-id;</a></td>
  </tr>
  </dtml-in>
</pre>

with id 'table_of_type' and title 'type_list'.

Now, replace all cloned copies of this code with the following:

<pre>
  <dtml-var "table_of_type(this(), _, type_list = ['FooType'])">
</pre>

If you want to call this method on objects elsewhere in the
hierarchy (i.e., not the "current" folder):

<pre>
  <dtml-with target>
    <dtml-var "table_of_type(this(), _, type_list = ['FooType'])">
  </dtml-with>
</pre>

where target is 'test', or '"PARENTS[-1].test"', etc.

--Jeff
-------------------------------------------------------------------