Freemaker Basic Tutorials

Source: Internet
Author: User
Tags html tags
Copy from http://demojava.iteye.com/blog/800204

The following are all collected online:

Freemarker template files are no more complex than HTML pages, Freemarker template files consist of the following 4 parts:
1, Text: Part of the direct output
2, note: <#--...--> format section, will not output
3, interpolation: That is ${...} Or #{...} Part of the format that replaces the output with the part in the data model
4,FTL directive: Freemarker specified, similar to HTML tags, name before plus # to differentiate, will not output

Here is an example of a freemarker template that contains the 4 parts described above
<title>Welcome!</title><br>
<body><br>
<#--Annotation Section--><br>
<#--use interpolation below-->
<p>we have these animals:<br>
<u1><br>
<#--Use FTL instruction-->
< #list animals as being><br>
<li>${being.name} for ${being.price} euros<br>
< #list ><br>
<u1><br>
</body><br>

1, FTL instruction rules

In Freemarker, using the FTL tag to use instructions, Freemarker has 3 FTL tags, which are exactly like HTML tags.
1, start tag:< #directivename parameter>
2, end tag: </#directivename >
3, empty label:< #directivename parameter/>

In fact, the symbol at the front of the label may also become @, and if the instruction is a user instruction instead of a system-built instruction, the # symbol should be changed to the @ symbol.
When you use the FTL tag, you should have the correct nesting, not the cross use, which is exactly the same as the XML tag. If you use a directive that does not exist, Freemarker does not use the template output, but instead produces an error message. Freemarker ignores whitespace characters in the FTL label. It is noteworthy that there is no white space character allowed between the <,/> and instructions.

2, interpolation rules

Freemarker interpolation has the following two types: 1, universal interpolation ${expr};2, number format interpolation: #{expr} or #{expr;format}

2.1 General interpolation

For universal interpolation, it can be divided into the following 4 kinds of situations:
1, interpolation result is string value: direct Output expression result
2, the interpolation result is a numeric value: The expression result is converted to the text output according to the default format (set by the #setting instruction). You can use built-in string functions to format a single interpolation, as in the following example:
< #settion number_format= "currency"/>
< #assign answer=42/>
${answer}
${answer?string} <#--the same as ${answer}-->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}
${answer}
The output results are:
$42.00
$42.00
42
$42.00
4,200%
3, the interpolation result is a date value: The expression result is converted to the text output according to the default format (set by the #setting instruction). You can use built-in string functions to format a single interpolation, as in the following example:
${lastupdated?string ("Yyyy-mm-dd HH:mm:ss zzzz")}
${lastupdated?string ("EEE, MMM D, ' yy")}
${lastupdated?string ("eeee, MMMM dd, yyyy, HH:MM:SS a ' (' zzz ')")}
The output results are:
2008-04-08 08:08:08 Pacific Daylight Time
Tue, APR 8, ' 03
Tuesday, April, 2003, 08:08:08 PM (PDT)
4, the interpolation result is a Boolean value: The expression result is converted to the text output according to the default format (set by the #setting instruction). You can format a single interpolation using built-in string functions, as in the following example:
< #assign foo=true/>
${foo?string ("Yes", "No")}
The output results are:
Yes

2.2 Number format interpolation

Numeric format interpolation can be formatted with #{expr;format}, where format can be:
MX: Decimal part minimum x bit
MX: Decimal part max x bit
As in the following example:
< #assign x=2.582/>
< #assign y=4/>
#{x; M2} <#--output 2.58-->
#{y; M2} <#--Output 4-->
#{x m2} <#--output 2.6-->
#{y m2} <#--output 4.0-->
#{x m1m2} <#--output 2.58-->
#{x m1m2} <#--Output 4.0-->

3, an expression

The expression is the core function of the Freemarker template, where the expression is placed in the interpolation syntax ${}, indicating that the value of the output expression is required; The expression syntax can also be combined with the freemarker tag to control the output. Actually, the Freemarker expression is very powerful, It supports not only the direct designation of values, output variable values, but also string formatting output and collection access functions.

3.1 Specifying values directly

Use the direct specify value syntax to let freemarker directly output the value in the interpolation, rather than the output variable value. The direct specified value can be a string, a numeric value, a Boolean value, a collection, and a Map object.

1, string
Specify string values directly using single or double quotes to qualify, if the string value contains special characters that need to be escaped, see the following example:
${"My files saved on c:\\ disk"}
${' My name is \ annlee\ '}
The output results are:
My files are saved on the C:\ disk.
My name is "Annlee."

Freemarker supports the following escape characters:
\ "; double quotes (u0022)
\ '; single quotation mark (u0027)
\ n, backslash (u005c)
\ n; line break (u000a)
\ r; carriage return (u000d)
\ t; Tab (u0009)
\b Backspace (u0008)
\f; Form Feed (u000c)
\l;<
\g;>
\a;&
\{; {
\xcode: Specifies the Unicode code directly through a 4-bit 16-digit number, outputting the character corresponding to the Unicode code.

If a paragraph of text contains a large number of special symbols, Freemarker provides another special format: You can add the R tag before the quotation marks in the specified string contents, and the file will be output directly after the R tag. Look at the following code:
${r "${foo}"}
${r "C:\foo\bar"}
The output results are:
${foo}
C:\foo\bar

2, numerical
The values in the expression are output directly, without quotation marks. Decimal use "." Delimited, the grouping "," symbol cannot be used. Freemarker currently does not support scientific notation, so "1E3" is wrong. Using numeric values in Freemarker expressions requires attention to the following points:
1, the value can not omit the decimal point before the 0, so ". 5" is the wrong wording
2, the value 8, +8, 8.00 are the same

3, Boolean value
Use True and false directly, and do not use quotes.

4, set
The collection is enclosed in square brackets, separated by commas "," between the elements of the collection, and look at the following example:
< #list ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] as X>
${X}
</#list >
The output results are:
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
Sunday

In addition, the collection element can also be an expression, as in the following example:
[2 + 2, [1, 2, 3, 4], "whatnot"]

You can also use a numeric range to define a set of numbers, such as 2. 5 is equivalent to [2, 3, 4, 5], but more efficient. Note that using a numeric range to define a collection eliminates the need for square brackets, and the range of numbers also supports the inverse of the range of digits, such as 5. 2

5,map objects
The Map object uses curly braces, which are delimited by the English colon ":" between the key-value pairs in the map, separated by the English comma "," for multiple sets of key-value pairs. Here is an example:
{"Language": 78, "math": 80}

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.