|
Assigning to Variables in the TemplateNormally, 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 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
List LiteralsAssignment 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 Note that a special kind of list literal was defined for version 2.0, the ranges. This is very useful for certain kinds of iterations. <assign range = 1..10> <assign range = list.start..list.end> Note that the range variables are most useful in iteration instructions, i.e. list or foreach. Note that the value or expression on both the left and right of the ".." operator must be integer values. Otherwise, an exception will be thrown. Hash LiteralsFinally, 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. Multiple AssignmentsA new feature of the FreeMarker 2.0 (Lazarus) release is that multiple assignments can occur within a single <assign .... > tag. <assign name = "John", age = 32, birthplace = "New York"> Note that the above example could be written in multiple lines for extra clarity. |
|