Variable Declaration

Variables can be defined globally, in pages (see PageVariables), and in code blocks.

Syntax:

var <identifier> : <Sort>;

This defines a variable within the current scope with name identifier and type Sort.

Variable declarations can also have an expression that initializes the value:

var <identifier> [: <Sort>] := expression;

The sort is optional in this case, if the Sort is not declared, the var will receive the type resulting from the expression (also known as local type inference).

Global variables always need an expression for initialization, they are added to the database once (when the first page is loaded, the database is checked to see whether all global vars have been created already). Global variables can be edited, but removing them can cause problems when there are explicit references to those variables.

Global variables can be further initialized using a global init{} block, e.g.

var defaultUser := User{name:="default"}
init{
  defaultUser.someInitializeFunction();
}
define page root(){
  output(defaultUser.name)
}

Global inits are also performed only once after database creation (if the dbmode is create-drop each new deploy will recreate the globals and execute inits, see ApplicationConfiguration).

The ; is optional for global and page variable declarations.