Previous Chapter | Next Chapter | Up | Next Section | Contents

Conditional Insertion, the dtml-if and dtml-unless Tags


Occasionally, the text to be included in a document is dependent upon some data. The if tag is provided to support the conditional insertion of text based on DTML variables or expressions. As described in "DTML Tag Syntax", the dtml-if tag has four forms:

  1. An dtml-if tag, with a closing /dtml-if tag,
  2. An dtml-if tag with an dtml-else tag and a closing /dtml-if tag.
  3. An dtml-if tag with one or more dtml-elif tags, an dtml-else tag, and a closing /dtml-if tag, and
  4. An dtml-if tag with one or more dtml-elif tags, no dtml-else tag, and a closing /if tag.

The dtml-if tag works in a straightforward manner. The variable or expression given in the dtml-if tag is evaluated. If the variable or expression value is true12, then the text following the dtml-if tag is inserted. If the variable or expression value is false, then for each dtml-elif tag given, the variable or expression given in the dtml-elif tag is evaluated. If an dtml-elif variable or expression value is true, the text following the dtml-elif tag is inserted and none of the following dtml-elif variables or expressions are evaluated. If there are no dtml-elif tags or if all of the dtml-elif tag variable or expression values are false, then the text following the dtml-else tag is inserted. If no dtml-else tag was supplied, then no text is inserted.

The dtml-if and dtml-elif tags support only the standard name and expr attributes. The dtml-else tag accepts no attributes.

In addition to the dtml-if tag, the dtml- unless tag is provided with its associated closing tag, /dtml-unless, to insert text if a condition is false. Like the dtml-if tag, the dtml- unless tag accepts the standard name and expr attributes:

<dtml-unless input_name>
You did not provide a name.
</dtml-unless>

Previous Chapter | Next Chapter | Up | Next Section | Contents