Using Local References

You can use local references in your web pages to create pages more efficiently.

Like standard references, local FML references always begin with the $ identifier.

The following examples are valid names for FML references:

$user_suggestion

$Usercalc

$Foo555

$Foo_bar

Creating Local References with #set

The #set command is used to set the value of a variable. For example:

 

#set ($animal = "bear")
#set ($bear = $brown)
 

 

A reference can be set to another variable, a string literal, the property of another FML reference, a method, a number, or a List.

For example:

 

## reference
#set( $coffee = $cup )

## string literal
#set( $book = "brown" )

## property reference. Note that $LocalPrice is a string.
#set( $LocalPrice = $Values.get('Price').FormattedResult)

##number literal
#set( $LocalPrice = 123 )

## List
#set( $VariableList = ["Price", "Sales", "Revenue"] )  
 

 

Math Functions in FML

You can also use #set for performing simple math on your web pages. More complex math should be performed inside the model.

For example:

 

#set( $value = $Run.SimStep + 1 )
#set( $value = $Run.EndStep - 1 )
#set( $value = $Values.get('Price').Result * 100 )
#set( $value = $foo * $bar )
#set( $value = $foo / $bar )
#set( $month = $Run.SimStep % 12 )
 

 

Using Strings

When using the #set directive, string literals that are enclosed in double quote characters.

Strings will be parsed and rendered, using double-quotes, as shown:

 

<html>
<body>

#set ($FirstName = "Sigmund")
#set ($LastName = "Freud")
#set ($FullName = "$FirstName $LastName")
#set ($NoSpaces = "$FirstName$LastName")

First Name = $FirstName <BR>
Last Name = $LastName <BR>
Full Name = $FullName <BR>
Without Spaces = $NoSpaces <BR>

</body>

</html>
 

 

The output will be:

 
First Name = Sigmund
Last Name = Freud
Full Name = Sigmund Freud
Without Spaces = SigmundFreud
 

 

However, when the string is enclosed in single quote characters, it will not be parsed:

Example:

 
<html>

<body>

#set ($FirstName = 'Sigmund')
#set ($LastName = 'Freud')
#set ($FullName = '$FirstName $LastName')
#set ($NoSpaces = '$FirstName$LastName')

First Name = $FirstName <BR>
Last Name = $LastName <BR>
Full Name = $FullName <BR>
Without Spaces = $NoSpaces <BR>

</body>

</html>                                                                                                               

 

 

The output will be:

 

First Name = Sigmund
Last Name = Freud
Full Name = $FirstName $LastName
Without Spaces = $FirstName$LastName  
 

 

Notice that with single quotes, $FirstName and $LastName are interpreted literally.

Strings can be set over multiple lines.  The following example is perfectly valid:

 

#set ($teststring =
"This is a test of
a multi-line
string.")

$teststring
 

 

The output of this example is (as you'd expect):

 

This is a test of
a multi-line
string.
 

 

Finally, note that you can combine strings with the + concatenation operator.  For example:

 

#set ($first_name = "King")
#set ($last_name = "George")
#set ($full_name = $first_name + " " + $last_name)

My name is $full_name.

 

will display:

 

My name is King George.
 

 

Of course, the same result could have been gotten without using the + operator with either of the examples below:

 

My name is $first_name $last_name.                                                                                                
 

 

 

#set ($first_name = "King")
#set ($last_name = "George")
#set ($full_name = "$first_name $last_name")

My name is $full_name.
 

 

But it's sometimes more convenient to use concatenation.