Normally, a template's data will come from its data model. However, sometimes it's convenient to assign a value to a variable in a template. You can do so using this syntax:
<assign variable = expression>
Dots aren't allowed in the name of the variable
you
assign to. The expression
can be any valid
expression. The equals sign is optional
syntactic sugar.
Assignment is mainly useful for making your templates more readable. For example:
<assign foo = "This is the constant value of foo"> <assign bar = some.long.variable.name>
The assignment is performed at run-time. In the second example, if
some.long.variable.name
is undefined, bar
will be
set to null.
Assign can also be used as a block statement, similar to JSTL. For example:
<assign foo> This is the value of the assignment. </assign> <assign bar> <p>Assign blocks can contain other FM-Classic content, such as ${inline} variables, or lists:</p> <list exampleList as example> List item ${example}<br> </list> </assign>
Assignment can also be used to assign a literal list of elements. For example:
<assign foolist = [ "one", "two", foo ]>
This will result in a list of one
, two
,
and This is the constant value of foo
(from the assignment
above).
Finally, assignment can be used to create a literal hash as well:
<assign foohash = { "one", foo, bar, "four" }>
The values in braces are pairs of values used to create the new hash. The first item of each pair will become the key of the hash, the second will become the value. Each key needs to be able to be evaluated as a scalar expression at runtime.
The key and value pairs can be separated with a colon (:), a comma (,) or an equals sign (=). For example, the earlier hash literal can also be written as:
<assign foohash = { "one" : foo, bar : "four" }>
A compile-time error will occur if the number of keys does not match the number of values inside the braces.
Both list literal and hash literal forms are syntactically similar to JSON formatted data.
Previous: Switch-Case | Next: Functions |