Riverwatcher Active Content Environment

RACE Documentation:


Flow Control
a. IF/ELSE
b. FOR loops
c. WHILE loops


if/else

RACE supports conditional branching via the "if" and "else" tags, e.g.:

<if test="<$age> < 10">
  You qualify for junior discount. <else if test="<$age> > 60">
  You qualify for the senior discount. <else>
  You don't qualify for any discount. </if>

Valid comparison operators for the if test include: ==, !=, <, <=, >, >=, in, not in, like, and not like. Since the equal operator (==) and friends are pretty self-explanatory, we will concentrate on the in and like operators.

The in operator takes two arguments, a string, a number or a scalar variable and a list of values. It will evaluate to 1 if the first argument is in the list of values, and 0 otherwise.

<if test="'<$name>' in ['joe', 'aaron', 'steve']">Found <$name> in the list.</if>

<if test="'<$name>' not in ['joe', 'aaron', 'steve']">Cannot find <$name> in the list.</if>

<if test="'<$name>' like '/jo*/'">Found a match for <$name>.</if>

<if test="'<$name>' not like '/jo*/'">Cannot find a match for <$name>.</if>

The "like" and "not like" operators take a string or a variable as its first argument and a regular pattern as its second argument. Learning how to use regular expression warrants a tutorial on its own. So, to not stray away from being a RACE tutorial, we will assume that you have some familiarity with regular expressions and will not go into any details here. The only thing to note here is that the regular expression needs be bracketed by slashes (/).

Of course, you can use compound logical expression with the help of the "or" and the "and" operators, e.g.:

<if test="(<$age> >= 21) and ('<$name>' not in ['joe', 'aaron', 'steve'])">Cannot find <$name> in the list.</if>

You can also use "||" instead of "or" and "&&" instead of "and" interchangeably, e.g:

<if test="(<$age> >= 21) && ('<$name>' not in ['joe', 'aaron', 'steve'])">Cannot find <$name> in the list.</if>



FOR loops

The for loop will iterate through a list of values and setting the variable named by the attribute "key" to each value, e.g.:

<for key="$name" list="['joe', 'aaron', 'steve']">
<$name>
<br>
</for>

The example above would output the three names, separated by line breaks. One very important caveat is that the variable denoted by the "key" attribute only exists while inside the for loop.

You can break out of a for loop with the exit tag, e.g.:

<for key="$name" list="['joe', 'aaron', 'steve']">
  <if test="<$name> == 'aaron'">
    <exit>
  <else>
    <$name>
<br>
  </if>
</for>




WHILE loops

A while loop will repeat its body for as long as the test returns true, or until the browser or the server times out, (i.e. if there is an infinite loop or the looping takes too long).

<define var="$index">1</define>
<while test="<$index> < 5">
  <$index.add(display)>
<br>
</while>

Any valid logical expression used in an if test can be used for the while loop. The only expression that you might not have seen would be the "add" macro. RACE has a set of built-in macros that you can apply to variables. You can find a complete list of the macros in the RACE Reference. The add macro will increment the value of index by 1, and the display parameter tells the add macro to return the result for display.

Just like the in the case of the for loop, you can break out a while loop with the exit tag, e.g.:

<define var="$index">1</define>
<while test="<$index> < 5">
  <$index.add(display)>
<br>
  <if test="<$fruits[$index]> == 'orange'"><exit></if>
</while>


The exit tag can also be used outside a loop to abort the execution of a RACE script. You can find out more about the exit tag in the RACE Reference.


RACE Documentation



 
Copyright 2009 Riverwatcher, Inc. Hosting by Riverwatcher Studios