I. Data Type:
Four basic types: String, number, Boolean, and date. The directly specified value can be a string, value, Boolean value, set, or map object.
1.String
Specify whether the string value is limited by single or double quotation marks. The Escape Character "\" can be used in the string "\". If a string contains a large number of special characters, you can add a letter R before the quotation marks, then all characters in the string will be directly output.
2.Value
Numeric values can be entered directly without quotation marks. Freemarker does not support scientific notation.
3.Boolean Value
Use true or false without quotation marks.
4.Set
The set is enclosed in parentheses, And the set elements are separated by commas.
A number range can also be used to represent a number set, such as 1 .. 5 is equivalent to the set [1, 2, 3, 4, 5]; you can also use 5 .. 1 indicates [5, 4, 3, 2, 1].
5. MapObject
Map Objects are enclosed in curly brackets. Key-value pairs in map are separated by colons, and multiple key-value pairs are separated by commas.
Note: Both the key and value of the map object are expressions, but the key must be a string. {& Quot; Name & quot;: & quot; green mouse & quot;, & quot; Price & quot;: 150}
6.Time object
Root. Put ("date1 ",NewDate ());
$ {Date1? String ("yyyy-mm-dd hh: mm: SS ")}
Ii. control statements
If command
This is a typical Branch Control Command. Its function is similar to if in Java. The syntax format of the IF command is as follows:
<# If condition>...
<# Elseif condition>...
<# Elseif condition>...
<# Else>...
</# If>
Example:
<# Assign age = 23>
<# If (age> 60)> elderly
<# Elseif (age> 40)> middle-aged
<# Elseif (age> 20)> young people
<# Else> young man
</# If>
Output: young people
The logical expressions in the above Code are enclosed in parentheses mainly because they contain the ">" symbol. Because freemarker regards the ">" symbol as the end character of the tag, a program error may occur, to avoid this, we should use parentheses wherever these symbols appear.
Custom commands (macro commands)
<# Macro M1> <# -- Define command M1 -->
<B> aaabbbccc </B>
<B> dddeeefff </B>
</# Macro>
<@ M1/> <@ m1/> <# -- call the preceding macro command -->
Output: aaabbbccc
Dddeeefff
Define macro commands with parameters:
<# Macro m2 a B C>
$ {A} -- $ {B} -- $ {c}
</# Macro>
<@ M2 a = "" B = "" c = ""/>
Nested command:
<# Macro Border>
<Table border = 4 cellspacing = 0 cellpadding = 4> <tr> <TD>
<# Nested>
</TD> </tr> </table>
</# Macro>
<@ Border> table content! </@ Border>
Include command:
<# Include "included.txt"/>
Iii. Set Operations
List, break
Syntax
<# List sequence as item>
...
<# If item = "Spring"> <# Break> </# If>
...
</# List>
Keywords
Item_index: subscript of the current value of list
Item_has_next: determines whether the list has a value.
Use Cases
<# Assign seq = ["Winter", "Spring", "Summer", "Autumn"]>
<# List seq as x>
$ {X_index + 1}. $ {x} <# If x_has_next>, </# If>
</# List>
Output
1. Winter,
2. Spring,
3. Summer,
4. Autumn
Iv. null value processing
The freemarker variable must be assigned a value; otherwise, an exception is thrown.
For freemarker, the null value is the same as that of a non-existent variable, because freemarker cannot understand the null value.
Freemarker provides two operators to avoid null values:
1 .! : Specifies the default value of the missing variable;
2 .?? : Determines whether a variable exists.
! There are two operators: Variable! Or variable! Defaultvalue. In the first method, if the default value is not specified for the variable, it indicates that the default value is an empty string, a set with a length of 0, or a map object with a length of 0.
Use variable! The defaultvalue operator specifies the default value. The default value type is not required to be the same as the variable type.
Test null value processing:
<# -- $ {SSS} does not define this variable. An exception is reported! -->
$ {Sss !} <# -- This variable is not defined. The default value is an empty string! -->
$ {Sss! "ABC"} <# -- this variable is not defined. The default value is string ABC! -->
5. namespace _ macro commands
When running the FTL template, there will be a set of variables created using the assign and macro commands (which may be empty). You can refer to the previous chapter on how to use them. A variable set like this is called a namespace. In simple cases, you can use only one namespace, which is called the main namespace. Because only the namespaces on this page are used, you are not aware of this.
If you want to create a set of macros, functions, and other variables that can be reused, it is usually referenced in terms
Library. It is inevitable to use multiple namespaces. You only need to consider whether you have a large macro set when you want to share it with others in some projects. However, make sure that there is no macro (or other variables) name in the library and the variables in the data model have the same name, and cannot have the same name as the variables referenced in other libraries in the template. Generally, variables conflict with each other because of name conflicts. Therefore, you must use different namespaces for the variables in each database.
Define the B. FTL file:
<# Macro copyright date>
<P> copyright (c) $ {date} Chinese people. </P>
</# Macro>
<# Assign mail = "[email protected]">
Introduce B. FTL in the. FTL file to use the macros and variables defined in B. FTL:
Test namespace:
<# Import "B. FTL" as BB/>
<@ BB. copyright date = "2010-2011"/>
$ {BB. Mail}
<# Assign mail = "[email protected]"/>
$ {Mail}
<# Assign mail = "[email protected]" in BB/>
$ {BB. Mail}
After execution, the console prints:
Test namespace:
<P> copyright (c) 2010-2011 Chinese people. </P>
[Email protected]
[Email protected]
[Email protected]
6. String operations
1.String connection
There are two syntaxes for string connection:
(1) use $ {...} Or # {...} to insert the expression value into the String constant;
(2) directly use the join operator "+" to connect to the string.
For example, the following two statements are equivalent:
$ {"Hello, $ {user }"}
$ {"Hello," + User + "! "}
Note that $ {...} can be used only for the text part as the interpolation output, but not for comparison and other purposes, such:
<# If $ {isbig}> wow! </# If>
<# If "$ {isbig}"> wow! </# If>
It should be written:
<# If isbig> wow! </# If>
2.Substring Truncation
The substring can be intercepted Based on the index of the string. If an index value is specified, the character at the index of the string is obtained. If two index values are specified, the substring in the middle of two indexes is truncated. For example:
& Lt; # assign number = "01234" & gt;
$ {Number [0]} <# -- output character 0 -->
$ {Number [0 .. 3]} <# -- output substring "0123" -->
7. built-in functions
Freemarker provides some built-in functions to convert the output, which can be followed by any variable ?.? Followed by built-in functions, you can use built-in functions to convert output variables.
Common built-in functions related to strings:
1. html: HTML encoding of strings;
2. cap_first: capital the first letter of the string;
3. lower_case: Convert the string to lowercase;
4. upper_case: Convert the string to uppercase;
Common built-in functions related to the set:
1. Size: obtains the number of elements in the set;
Common built-in functions related to numeric values:
1. INT: Get the integer part of the number.
Example:
Root. Put ("htm2", "<B> bold </B> ");
Built-in functions:
$ {Htm2? HTML}
Output: <B> bold </B>
Introduction to frequently used freemarker commands