Next Chapter | Up | Next Section | Contents

The dtml-else Tag as an Intermediate Tag in the dtml-in Tag


In the previous example (figure 2) an dtml- in tag is combined with an dtml-if tag to avoid showing an empty table for an empty sequence of employees. An alternative approach is to use an intermediate dtml-else tag in the dtml-in tag. If an dtml-in tag has an intermediate dtml-else tag, then the text following the dtml-else tag is inserted if the sequence used in the dtml-in tag is empty. Figure 4 shows DTML source which uses an dtml-else tag in the dtml-in tag to avoid showing an empty table. The output from this source is the same as the output from the source shown in figure 3. The source in figure 4 is actually more complex that the source in figure 3. The added complexity is due to the fact that the table header and footer have to be moved inside the dtml-in tag. Furthermore, the insertion of the table header and footer has to be conditioned on whether or not an item is the first item, last item, or neither by using the variables sequence-start and sequence-end (table 19).

DTML source to create an employee phone listing which properly handles the case of no employees by using an dtml-else tag in an dtml-in tag.

<dtml-in employees sort=name>
<dtml-if sequence-start>
<table>
<tr><th>Name</th><th>Phone number</th></tr>
</dtml-if>
<tr>
<td><dtml-var name></td>
<td><dtml-var phone></td>
</tr>
<dtml-if sequence-end>
</table>
</dtml-if>
<dtml-else>
Sorry, there are no employees.
</dtml-in>

 

 

When a name attribute is used in an dtml-in tag within an if tag, the sequence is only evaluated once, since the dtml-if tag caches the value associated with a name attribute.

In most cases, it is best to use an dtml-in tag inside an dtml-if tag, as illustrated in figure 3. One case in which it may be best to use an dtml-else tag within an dtml-in tag is when the sequence used by the dtml-in tag is computed using an expr attribute and the computation is expensive. Use of the dtml-else tag in the dtml-in tag avoids having to define and evaluate the expression twice.

 

Next Chapter | Up | Next Section | Contents