Choosing Text to Display with #if

The #if directive in FML allows for text to be included when the web page is generated, on the condition that the if statement is true. For example:

 

#if($Values.get('Our Sales').Result < 1000 )
Sales have fallen below 1000!<BR>
#end
 

 

In this case, if Our Sales is less than 1000, the output will be:

 

Sales have fallen below 1000!
 

 

Conversely, if Our Sales is equal to or greater than 1000, the statement evaluates as false, and there is no output.

 

The variable is evaluated to determine whether it is true, which will happen under one of two circumstances:

  1. the variable is a boolean (true/false) which has a true value, or

  2. the value is not null.

The content following the #if directive  becomes the output if the evaluation is true.

 

An #elseif or #else directive can be used with an #if directive.

Forio Macro Language will stop at the first expression that is found to be true.

In the following example, suppose that your model's EndStep has a value of 15 and it's SimStep has a value of 6.

 

#if($Run.EndStep < 10 )
Go North
#elseif($Run.EndStep > 10 )
Go East
#elseif($Run.SimStep == 6 )
Go South
#else
Go West
#end
 

 

In this example, if $Run.EndStep is greater than or equal to 10, FML makes the output of the if statement Go East. If $Run.EndStep has a value of 10 and $Run.SimStep had a value of 7, then $Run.EndStep is neither greater than 10 nor equal to 10, and $Run.SimStep is not equal to 6. All if and #elseif statements are false, so the output is "Go West".

Checking if a Reference is Defined

The #if directive can also be used to see if a reference is defined.  For example, the following FML checks to see if the user is currently doing a trial run of the simulation.  The reference $Trial is only defined if a trial is currently in progress.

 

#if($Trial)
Your free trial subscription has $Trial.DaysLeft left of $Trial.Days
#end
 

 

Relational and Logical Operators

FML uses the equivalent operator to determine the relationships between variables. Here is a simple example to illustrate how the equivalent operator is used.

 

#set ($foo = "black")
#set ($bar = "white")

#if ($foo == $bar)
In this case it's clear they aren't equivalent.
#else
They are not equivalent and this will be the output.
#end

 

 

FML supports standard logical operators, allowing you to compare numbers or strings.  Logical operators have a symbol but can also be spelled out explicitly.  This allows you to use the operators in XML documents where some of the symbols (for example '>') are banned.

Operator Name

Symbol

Alternative Symbol

Example

Equals Number

==

eq

#if( $foo == 42 )

Equals String

==

eq

#if( $foo == "bar" )

Object Equivalence

==

eq

#if( $foo == $bar )

Not Equals

!=

ne

#if( $foo != $bar )

Greater Than

>

gt

#if( $foo > 42 )

Less Than

<

lt

#if( $foo < 42 )

Greater Than or Equal To

>=

ge

#if( $foo >= 42 )

Less Than or Equal To

<=

le

#if( $foo <= 42 )

Boolean NOT

!

not

#if( !$foo )

 

Note that in most cases you will be comparing a model result to a number.  Be sure to use the Result property in order to compare the actual result.  The first example below will not work.  The second example properly uses the Result property and will correctly check the value.  

 

#if($Values.get("Sales") == 0)
 This is wrong!  Will never be shown.
#end

 

#if($Values.get("Sales").Result == 0)
 No Sales!
#end
 

 

You can also use the getResult() method to compare a number to a past result.

 

#set($lastStep = $Run.SimStep - 1)
#if($Run.SimStep > 0 && $Values.get("Sales").Result > $Values.get("Sales").getResult($lastStep))
Sales have gone up!
#end

 

 

AND, OR, and NOT Operators

You can use AND, OR, and NOT operators between expressions within the #if directive.  Each operator has a standard symbol and a spelled-out alternative.

For example:

 

#set ($first = 5)
#set ($second = 12)
#set ($third = 27)
#set ($fourth = false)

#if($first == 5 && $second == 12)
This statement will be displayed because $first = 5 AND $second = 12.
#end

#if($first == 7 || $second == 12)
This statement will be displayed because $second = 12 (either the first OR the second expression must be true.
#end

#if($third != 15)
This statement will be displayed because $third is NOT equal to 15.
#end

#if(!$fourth)
This statement will be displayed because NOT reverses what follows.
#end

 

 

AND operator

The AND operator uses two ampersands: &&.  You can also use the word "and".

AND returns TRUE if both arguments are TRUE; returns FALSE if one or more arguments is FALSE.

Example:

 
#set ($first = 5)
#set ($second = 12)
#if($first == 5 && $second == 12)
This statement will be displayed because $first = 5 AND $second = 12.
#end
 

 

OR operator

The OR operator uses two bars: ||.  You can also use the word "or".

OR returns TRUE if any argument is TRUE; returns FALSE if all arguments are FALSE.

Example:

 
#set ($first = 5)
#set ($second = 12)
#if($first == 7 || $second == 12)
This statement will be displayed because $second = 12 (either the first OR the second expression must be true.)
#end
 

 

NOT operator

The NOT operator uses an exclamation: !.  You can also use the word "not".

NOT reverses the value of its argument. Use NOT when you want to make sure a value is not equal to one particular value.

Example 1:

 
#set ($third = 27)
#if($third != 15)
This statement will be displayed because $third is NOT equal to 15.
#end
 

 

Example 2:

 
#set ($fourth = false)
#if(!$fourth)
This statement will be displayed because NOT reverses what follows.
#end
 

 

false and true

You can also type the words true and false directly in FML and Broadcast will interpret them correctly. false returns the logical value for FALSE and true returns the logical for TRUE.

Note: false and true must always be all lower-case.

Example 1:

 
#if(true)
This statement will be displayed because its is true.
#end
 

 

Example 2:

 
#if(!false)
This statement will be displayed because the reverse of false is true.
#end