Design Freemarker Design Guidelines (2)
2. Data Model
(1) Basic
L introduced the three basic object types used in templates in QuickStart: scalars, hashes, and sequences, but there are other capabilities:
Øscalars: Store Single value
Øhashes: acts as a container for other objects, each associated with a unique query name
Øsequences: acts as a container for other objects, in order to access
Ø method: Calculate by passing parameter, return result with new object
Ø user-defined FTL tags: macros and converters
L usually each variable has only one of these capabilities, but a variable can have several of these capabilities, as in the following example:
(Root)
|
+-mouse = "Yerri"
|
+-Age = 12
|
+-color = "Brown" >
L Mouse is both scalars and hashes, merging the data model above into the following template:
${mouse} <#--use mouse as scalar-->
${mouse.age} <#--use mouse as hash-->
${mouse.color} <#--use mouse as hash-->
L Output results are:
Yerri
12
Brown
(2) Scalar variable
L scalar variable store single value, can be:
Ø string: Simple text, enclosed in a template with quotes (either single or double quotes)
Ø numbers: Use numeric values directly in the template
Ø Date: Store date/Time related data, can be date, time or date-time (Timestamp); Typically, date values are added to the data model by programmers, and designers only need to show them
Ø Boolean value: True or FALSE, usually used in < #if ...> tags
(3) Hashes, sequences and collection
L Some variables do not contain anything that can be displayed, but they contain other variables as containers, and there are two types of them:
Øhashes: Has a unique query name associated with each variable it contains
Øsequences: associated with a number and each variable it contains, index value starting from 0
L A collection variable is usually similar to sequences unless its size is inaccessible and the index cannot be used to obtain its child variables; The collection can be viewed as restricted sequences that can only be used by the < #list ...> directives
(4) method
The L method variable is usually based on the given parameter computed value
L The following example assumes that the programmer has placed the method variable avg in the data model to calculate the numeric average:
The average of 3 and 5 is: ${avg (3, 5)}
The average of 6 and is: ${avg (6, 10, 20)}
The average of the price of Python and Elephant are: ${avg (Animals.python.price, Animals.elephant.price)}
(5) Macros and converters
L Macros and converter variables are user-defined directives (custom FTL tags) that describe these advanced features later
(6) Node
The L-node variable is represented as a node in the tree structure, usually used in XML processing, and is described in a later special section
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.