Next Chapter | Up | Next Section | Contents

The else Tag as an Intermediate Tag in the in Tag


In the previous example (figure See DTML source to create an employee phone listing which properly handles the case of no employees by using an in tag with an if tag.) an in tag is combined with an if tag to avoid showing an empty table for an empty sequence of employees. An alternative approach is to use an intermediate else tag in the in tag. If an in tag has an intermediate else tag, then the text following the else tag is inserted if the sequence used in the in tag is empty. Figure See DTML source to create an employee phone listing which properly handles the case of no employees by using an else tag in an in tag. shows DTML source which uses an else tag in the 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 See DTML source to create an employee phone listing which properly handles the case of no employees by using an in tag with an if tag.. The source in figure See DTML source to create an employee phone listing which properly handles the case of no employees by using an else tag in an in tag. is actually more complex that the source in figure See DTML source to create an employee phone listing which properly handles the case of no employees by using an in tag with an if tag.. The added complexity is due to the fact that the table header and footer have to be moved inside the 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 See Item variables defined by the in tag).

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

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

 

 

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

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

 

Next Chapter | Up | Next Section | Contents