If Tag

Use the if tag to conditionally include part of a report when creating document templates. You may also use the else tag within the body of the if tag to include a different section of the report when the condition is not met. Be sure to insert both the start and end tags.

Suppose you want your report to display different things depending upon the data. For example, you want the city name to be displayed only if the city is in North America.

You can use the if tag to do this. The if tag is known as a conditional tag, and using the if tag boils down to two actions:

  • Setting a condition.
  • Determining what to do if the condition is met.

In simple terms, the if tag says, "If the condition that I set is met, then perform a particular action."

It isn't enough to simply determine whether a condition is met; you must also tell the template what to do if the condition is met.

Refer to your contact list table. First, you insert an if tag that says, "See if the customer's city is in North America." Then, you tell the report to display the city name if the condition is met.

If the condition is met in part one, 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 one that 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.

Here's the table in our template:

First Name

Last Name

City

<forEach tag> <out tag>

<out tag>

<if tag> <out tag> <endIf tag>

<endforEach tag>

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 (example: display a piece of data)
  • The endIf tag tells the template to stop setting a condition

And here's the report:

First Name

Last Name

City

Lisa

Harris

En-Jay

Hsu

New York

Tomas

Ramirez

Kamala

Sharma

If Tag Attributes

  • select - required (unless test is defined). The node that will be tested for existence. An empty node is considered to exist.
  • notEmpty - optional (only used with the select attribute). The node must exist and must not be empty.
    In the if tag you can set the notEmpty property to true or false. This property is only used for a SQL select that returns null or an XPath select that returns a zero length string. For the case of returning a node that exists but is empty:
    • notEmpty = true - the if tag will return false because the node must not be empty to return true.
    • notEmpty = false - the if tag will return true because the node exists and you are not requiring it be empty.
    • test - required (unless select is defined). The boolean statement to evaluate.
      Valid operators for the test="" attribute are:

      ()

      +

      -

      *

      /

      div

      %

      mod

      ==

      eq

      !=

      ne

      <

      >

      <=

      le

      >=

      ge

      &&

      and

      ||

      or

      !

      not

Valid variables are:

  • boolean (the text true or false - no quotes)
  • long
  • double
  • string - strings must be in quotes. You may include \" inside a string, but no other escape sequences.

forEach Row Test

A forEach loop can iterate over several rows of data at once (see the forEach tag for details on this). Inside the loop row [0] always exists but the additional rows may not exist in the last iteration of the loop. For example, if the forEach is iterating over 7 rows of data and has step=’3’ then the first two times through the loop all 3 rows exist but the last time only the first row exists.

Rows access is 0-based. So the first row is [0] and the second row is [1]. The number inside the [] is the offset from the start and the first row is the start of the rows.

To test if a row exists, you use the following construct. Note that this is a test=, not a select=.

<wr:forEach ="./name" var=”items” step=”3”>

<wr:if test=”${@items[1]}”>

Second row exists

</wr:if>

</wr:forEach>

The @items[1] is the context for row [1] in the forEach loop. By definition this if will always be true for each time through the loop except the last time. Also, all other tags will return nothing if they are for a row that does not exist, so in many cases you do not need to use this if.

All row access aside from this use in if uses items[1] while this uses @items[1]. The @ sign tells the system to check existence (a true/false value) instead of access the row’s data.