FreeMarker Design Guide (2)
2, data model
(1) foundation
l Type three basic object types used in the template in a quick entry: Scalars, Hashes and Sequences, in fact, there can be more than other capabilities:
Ø SCALARS: Storage single value
Ø Hashes: A container that acts as other objects, each contains a unique query name
Ø SEQUENCES: A container that acts as other objects is accessed in order
Ø Method: Calculated by passing the parameters, return the result with the new object
Ø User Custom FTL Tag: Macro and Converter
l Usually each variable has only the above capabilities, but a variable can have a plurality of the above capabilities, as described below:
(root)
|
- mouse = "Yerri"
|
- AGE = 12
|
- Color = "brown">
l Mouse is both SCALARS and HASHES, merged the above data model to the following template:
$ {mouse} <# - use mouse as scalar ->
$ {mouse.age} <# - use mouse ashash ->
$ {mouse.color} <# - use mouse ashas ->
l The output result is:
Yerri
12
Brown
(2) SCALAR variable
l Scalar variable storage single value, it can be:
Ø String: Simple text, enclose the use of quotation marks (single quotes or double quotes) in the template
Ø Number: Digital value directly in the template
Ø Date: Storage Date / Time-related data, can be date, time or date - time (timestamp); usually, the date value is added by the programmer to the data model, and the designer only needs to display them.
Ø Boolean: True or False, usually used in <#if ...> tags
(3) Hashes, Sequences and Collection
l Some variables do not contain any displayable content, but as a container containing other variables, there are two types:
Ø HASHES: Has a unique query name and each variable it contains
Ø SEQUENCES: Use the number and each variable it contains, the index value starts with 0
l The set variables are usually similar to SEQUENCES unless it cannot access its size and cannot use the index to get its sub-variables; the collection can be seen as a limited SEQUENCES that can only be used by <#List ...> instructions
(4) method
l method variables are typically based on the given parameter calculation value
l The following example assume that the programmer has placed the method variable AVG in the data model to calculate the digital average:
The Average of 3 and 5 IS: $ {AVG (3, 5)}
The average of 6 and 10 and 20 IS: $ {AVG (6, 10, 20)}
The average of the price of python and elephant is: $ {avg (animals.python.price, animals.ephant.price)} (5) macro and converter
l Macro and transducer variables are user-defined instructions (custom FTL tags), which will tell these advanced features later.
(6) node
l Node variables are represented as a node in the tree structure, usually used in XML processing, which will be described in later special chapters.