417 users using DBSP now!
home -> documentation -> environment guide -> Making decisions: The IF tag

Making decisions

Making decisions is a fundamental concept in the most of the web applications. We need to take decisions of what options or information we need to present to the user. Thus, we need the ability to make comparisons between variables, constants and expressions. So, the first step is to learn how to make comparisons.

Making comparisons

As we see in the Section 6.6, some DBSP elements have the ability to evaluate arithmetic operations using the EXPRESSION property. However, the evaluator is not restricted to arithmetic operations only; it could evaluate Boolean expressions resulting in true or false. These expressions will be the basis of the decision taking in our systems. See the EXPRESSION documentation for more information about this property.

The IF Tag

The IF Tag condition a block of code to the result of a comparison, defined in the expression property. If the result is TRUE, the block is inserted; otherwise it is not.

The IF Tag, in its simplest configuration, is of the form:

<#IF EXPRESSION="A>B">
  ...
<#/IF>

where the EXPRESSION property must be a Boolean expression.

The else clause

We can extend the basic IF Tag by adding an ELSE tag before the IF closing tag. This tag provides a second block choice, when the result of the expression is false. The syntax of this tag is of the form:

<#IF EXPRESSION="A>B">
  ...
<#ELSE>
  ...
<#/IF>

This provides an explicit choice between two courses of action - one if the if expression is true and another for when it is false.

Nested IF tags

The block code executed when an IF tag is true can contain another IF tag, in the IF or ELSE block. An IF tag that is nested inside another, can also itself contain a nested IF. There is no limit on the number nested IF tags. However, is not the best practice because it adds confusion to the application logic.

Here is an example of a nested IF tag

<#IF EXPRESSION="A>B">
  <#IF EXPRESSION="A>B">
    ...
  <#ELSE>
    ...
  <#/IF>
<#ELSE>
  ...
<#/IF>

Even when multiple decision branches could be taken with nested Ifs, there is a much simpler way to achieve the same result, as we will see in the next section.

Examples

Example 1. A simple IF tag

This example shows the most basic use of the IF Tag.

Source code
Test example


Example 2. A random IF statement

This example shows another variant of the IF Tag.

Source code
Test example


Example 3. A random nested IF statement

This example shows a nested IF Tag.

Source code
Test example

Previous | Next