Declare variables

Variables must be declared before they can be used. The declaration defines the type of the variable, which restricts the kind of content that can be assigned and an initial value.

A variable declaration has the following form.

<type> <name>

<type> <name> := <initial value>

The <type> specifies the type of values that can be assigned to the variable. For a description of available data types, see Types.

The <name> must meet the following rules for all variable types except FIELDSET and DATASTRUCTURE:

  • The first character must be a lowercase letter.
  • The following characters must be lowercase letters, digits, or underscores.
  • Only letters from the Latin-1 character set are allowed.

For FIELDSET and DATASTRUCTURE variables, the <name> must meet the following rules:

  • The first character must be an uppercase letter.
  • The following characters must be letters, digits, or underscores. Both uppercase and lowercase letters are allowed.
  • Only letters from the Latin-1 character set are allowed.

An example is provided here.

TEXT saluation
NUMBER next_year
BOOL single_customer
FIELDSET PartnerData
MAP NUMBER stock_portfolio

The variable can be initialized with a value as part of the declaration. This initial value must have the appropriate type for the variable. If the initial value is omitted, a default value is assigned:

TEXT variables are initialized with an empty value.

NUMBER variables are initialized with the value 0.0.

BOOL variables are initialized with the value FALSE.

MAP and FIELDSET variables are initialized without any values.

DATASTRUCTURE variables use the default values for their members as described earlier in this section. Members that are a DATASTRUCTURE themselves are undefined.

An example is provided here.

TEXT saluation := "Dear"
NUMBER next_year := today + 10000
BOOL preferred_customer := (_data.NumberOfOrders >= 10)
FIELDSET Partner := _data.Partner	
MAP NUMBER stock_portfolio := customer_previous_portfolio

Variables have a scope that is limited to the code block in which they are declared. When this block ends the variable is not accessible anymore. Variable names must be unique within the block in which they are defined. It is allowed to define variables with the same name in different blocks; when blocks are nested a variable in the inner block hide the variable in the outer block.

The following keyword pairs define blocks in the Template scripting language:

  • BEGIN until END
  • DO until OD
  • REPEAT until UNTIL
  • IF until the next ELIF, ELSE, or FI
  • ELIF until the next ELIF, ELSE, or FI
  • ELSE until FI

An example is provided here.

NUMBER var
IF var > 0 THEN
	#
	First Inner scope: @(var)
	#
	ASSIGN var := 42 (* Declared outside the IF..FI *)

	TEXT var := “Hello World” (* Hides the NUMBER var from here. *)

	#
	Second Inner scope: @(var)
	#
FI (* End of scope *)
#
Outer scope: @(var)
#

This example will produce the following result.

First Inner scope: 0.00
Second Inner scope: Hello World
Outer scope: 42.00