How Do I Use If, Else and EndIf Tags?
This is a high level overview explaining If and Else Tag concepts. For more information about If and Else Tag syntax and properties, see the If Tag Reference.
How If and Else Tags Work
An If Tag is basically a Boolean expression, or condition, such as "${varName} <= 10"
(${varName} is less than or equal to 10), whose value determines whether the contents of the If Tag are processed. If the condition evaluates to 'true,' then the content between the If Tag and the EndIf Tag are processed. If the condition evaluates to 'false,' then the content between the If Tag and the EndIf Tag are ignored.
For example, if you only want to see prices for items that show a cost that is less than or equal to 10, you could display only those prices using a condition such as "${CostOfItems} <= 10” and ignore the cost of other items in the list.
Since the If tag is only processed when the condition evaluates to 'true,' users can use an Else tag to display content in the case that the If tag evaluates to 'false.' For example, you may wish to print "the cost of this item is less than or equal to 10" if "${CostOfItems} <= 10" evaluates to 'true;' otherwise print "the cost of this item is greater than 10."
If you wish to perform one of several actions depending on the value of a condition, use the Switch and Case Tags.
Specific to If and Case Tags, the notEmpty property can be set to 'true' or 'false' (the default). This property determines what happens if a SQL query returns NULL or an XPath or JSONPath query returns a zero-length string.
If a row or node is returned that exists but is empty or NULL:
If notEmpty is set to 'true', then the If Tag's condition will return 'false', since the node must not be empty to return 'true'.
If notEmpty is set to 'false', then the If Tag's condition will return 'true', since the node exists and you are not requiring it to be non-empty.
If Tag Example
Suppose you want your report to display different things depending upon the data. For example, in an employees table, let's say we want the city name to be displayed only if the city is in North America. You can use the If Tag to do this.
Let's refer to an employee list table. The table below was made with a ForEach Loop and Out Tags to display employee names and cities. Here's what the Template and Output look like without an If Tag:
Now let's say we want to display the city name only if the city is in North America.
- Insert an If Tag that says, "See if the customer's city is in North America."
- Tell the report to display the city name only if the condition is met.
This second step can be a bit tricky to understand, so let's reiterate. If the condition is met in step one ("See if the customer's city in is North America"), you have to explicitly tell the template what to do next: display the city name.
This may seem like an extra step, but it's a vital one – and it's what gives the If Tag great flexibility and power. For example, you could tell the template that if the city is in North America to insert an image of the Western Hemisphere; to include a statement for residents in certain time zones; or to perform any number of actions.
The If Tag will continue to act (just like the ForEach Tag continues to act) until it reaches an EndIf Tag.
Let's see this in action. Here's the table in our template before generating output.
There are three tags in the City column:
- The If Tag sets the condition
- The Out Tag tells the report what to do if the condition is met (i.e. display a piece of data)
- The EndIf Tag tells the If Tag to stop Note that the If Tag and EndIf Tags are on either side of our Out Tag for the City, and are set within a ForEach Tag loop.
And here's the output:
Else Tag
An Else Tag is optional, and it is used in conjunction with an If Tag. You can have an If Tag without an Else Tag, but you can't have an Else Tag without an If Tag.
The Else Tag adds increased power to the If Tag. By default, the If Tag has a third part that is understood but not explicitly stated: if the condition isn't met, do nothing.
You can change this, however. If the If Tag's condition isn't met, you can tell the report template to do something else by using the Else Tag. For example, the report could insert the text "N/A" for cities outside the continent.
Now here's the table with an Else Tag:
There are four tags in the city column:
- the If Tag sets the condition
- the Out Tag tells the report what to do if the condition is met
- the Else Tag tells the report that if the condition set by the If Tag isn't met, to do something else; in this case, insert the text "N/A"
- the EndIf Tag tells the template to stop setting a condition
And here's the final report: