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.
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.
A compile-time error will occur if there is not an even number of values inside the braces.
| Previous: Switch-Case | Next: Functions |