[Zope] [ZGotW] Issue #1 Entry

Zope Guru of the Week ZGotW@palladion.com
2000/01/21 07:26:15.6964 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>
  &lt;dtml-in &quot;objectValues(type_list)&quot;&gt;
  &lt;tr&gt;
    &lt;td&gt;&lt;a href=&quot;&amp;dtml-id;&quot;&gt;&amp;dtml-id;&lt;/a&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/dtml-in&gt;
&lt;/pre&gt;

with id 'table_of_type' and title 'type_list'.

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

<pre>
  &lt;dtml-var &quot;table_of_type(this(), _, type_list = ['FooType'])&quot;&gt;
</pre>

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

<pre>
  &lt;dtml-with target&gt;
    &lt;dtml-var &quot;table_of_type(this(), _, type_list = ['FooType'])&quot;&gt;
  &lt;/dtml-with&gt;
</pre>

where target is 'test', or '&quot;PARENTS[-1].test&quot;', etc.

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