Variables
ftd
has support for rich data modelling, and it supports declaring variables.
A variable is a named storage that programs can manipulate.
-- integer x: 20
Variables have type
s.
Types
The data type is mandatory while declaring a variable in FTD.
Type of a variable can be one of the built-in types, a record, or an or-type.
Immutable
By default, variables are immutable − read only in FTD. In other words, the variable’s value cannot be changed once a value is bound to a variable name.
-- integer x: 10 -- ftd.integer: $x $on-click$: $ftd.increment($a = $x)
The output will be as shown below:
Cannot have mutable reference of immutable variable `x`
The error message indicates the cause of the error - Cannot have mutable reference of immutable variable x
. This is one of the many ways FTD allows programmers to write code and takes advantage of the safety.
Mutable
Variables are immutable by default. Prefix the variable name with $
to make it mutable. The value of a mutable variable can be changed.
The syntax for declaring a mutable variable is as shown below −
-- integer x: 10 -- ftd.integer: $x $on-click$: $ftd.increment($a = $x)
The output will be as shown below: Click on 10
.
Output
10
Referring To Another Variable
A variable can be defined as referring to another variable. This means if the referenced variable get changed the referer will change too.
-- integer $x: 10 -- integer y: $x -- ftd.integer: $x -- ftd.integer: $y -- ftd.text: I change x $on-click$: $ftd.increment($a = $x)
Here, if x changes, y also changes.
The output will be as shown below: Click on I change x :)
to see the result.
Output
x:
10
y:
10
I change x :)
Clone the value of a Variable
A value of the variable can be cloned by de-referencing the variable reference. This means that cloning creates a duplicate value and if the cloned value changes, the object, that clones it, will not change.
-- integer x: 10 -- integer y: *$x -- ftd.text: I change x :) $on-click$: $ftd.increment($a = $x)
Here, if x changes, y doesn’t changes.
The output will be as shown below: Click on I change x :)
to see the result.
Output
x:
10
y:
10
I change x :)
Updating a Variable
Once a mutable
variable has been defined it can be updated too. Any variable can be made mutable
by prefixing it with $
.
Note: By default, ftd variables
are immutable
(can’t be changed once initialized).
-- integer $x: 10 -- $x: 20
The type of the variable can not be updated.
$processor$
: dynamic variables
ftd
documents are processed in the context of a “platform”, and platform can provide access to dynamic variables.
Say platform has provided a dynamic variable os
, which is the operating system on which this document is getting rendered, you can use that like this:
-- string name-of-os: $processor$: os
type
is mandatory when using $processor$
. Available processors would be documented as part of platform documentation.
Processors can also look at data passed, so its possible to create a processor:
-- string greeting: hello, world $processor$: uppercase
Say the platform has provided a processor uppercase
, which takes the current value, hello, world
and returns its upper case value. In this case the variable greeting will hold the value: HELLO, WORLD
.
Foreign variables
Like $processor$
, the platform provides foreign variables against a module. The FTD stuck itself to fetch value of foreign value, which is, then, provided by platform.
The most common foreign variable is assets
-- import: module/assets -- ftd.image: src: $assets.files.images.image1