Freemarker built-in function usage notes

Source: Internet
Author: User
Tags date1

Freemrker contains a number of functions, such as manipulating strings, numbers, collections, etc. that can be conveniently processed on the page.

Sequence built-in functions

1. Sequence?first returns the first value of a sequence.

2. Sequence?last returns the last value of the sequence.

3. Sequence?reverse reverses the existing order of the sequence, which is the reverse sort

4. Sequence?size returns the size of the sequence

5. Sequence?sort to convert objects in sequence to strings after ordering

6. Sequence?sort_by (value) is sorted by the property value of the object in sequence

Note: sequence cannot be null. The above method in my other blog freemarker How to traverse the list has a detailed application, interested friends can refer to.

Two, the built-in function of hash

1. Hash?keys returns all keys in the hash and returns the result as sequence

2. Hash?values returns all value in the hash and returns the result as sequence

For example:

< #assign user={"name": "Hailang", "Sex": "Man"}>
< #assign keys=user?keys>
< #list keys as key>
${key}=${user[key]}
</#list >

Third, operation string function

1. SUBSTRING (start,end) intercepts substrings from a string
Start: Intercept the index at which the substring starts, start must be greater than or equal to 0, less than or equal to end

End: Truncate the length of the substring, end must be greater than or equal to 0, less than or equal to the length of the string, if omitted, the string length is the default.

Example:

${' str '? SUBSTRING (0)}à results for Str

${' str '? SUBSTRING (1)}à result is TR

${' str '? SUBSTRING (2)}à result R

${' str '? SUBSTRING (3)}à result is

${' str '? SUBSTRING (0,0)}à results for

${' str '? SUBSTRING (0,1)}à result is s

${' str '? SUBSTRING (0,2)}à Results for St

${' str '? SUBSTRING (0,3)}à results for Str


2. Cap_first the first word in the string into uppercase.
${' str '? Cap_first}à results for Str

3. Uncap_first the first word in the string into lowercase.
${' Str '? Cap_first}à results for Str

4. Capitalize the first letter of all words in a string into uppercase
${' str '? Capitalize}à results for Str

5. Date,time,datetime convert a string to a date
For example:

< #assign date1= "2009-10-12"? Date ("Yyyy-mm-dd") >
< #assign date2= "9:28:20"? Time ("HH:mm:ss") >
< #assign date3= "2009-10-12 9:28:20"? Time ("HH:mm:ss") >

${date1}à Results for 2009-10-12
${date2}à Results for 9:28:20
${date3}à Results for 2009-10-12 9:28:20

Time formatting: ${book.date?string (' Yyyy-mm-dd ')}

Judging length:< #if oa.content?length gt= 20>

Note If the specified string is malformed, an error is raised.

6. Ends_with determines whether a string is terminated by a substring and returns a Boolean value.

${"string"? Ends_with ("ing")? String} Returns a result of true

Note: The Boolean value must be converted to a string to output

7. HTML is used to replace <, >, &, and "in the string with the corresponding &lt;&gt;&quot:&amp

8. Index_of (Substring,start) finds a substring in a string, returns the index of the first character of the substring found, and returns 1 if no substring is found.

The start parameter is used to specify that the search starts at that index of the string and start is a numeric value.
If start is greater than the string length, the start value equals the string length, and if start is less than 0, start takes a value of 0.
${"string"? Index_of ("in") à result is 3

${"string"? Index_of ("ab") à result is-1

9. Length return string ${"string"? Length}à result is 6

Lower_case. String to lowercase

${"string"? Lower_case}à result is string

One by one. Upper_case string to uppercase

${"string"? Upper_case}à result is string

Contains determines whether a substring is contained in a character. Returns a Boolean value

${"string"? Contains ("ing")? String}à result is True
Note: The Boolean value must be converted to a string to output

Convert a string to a number

${"111.11"? Number}à result is 111.11

Replace replaces a part of a string with a different string from left to right.

${"STRABG"? Replace ("AB", "in")}à result is string

Split a string into a set of strings using the specified delimiter

< #list "This|is|split" Split ("|") as s>
${s}
</#list >

The result is:
This
Is
Split


Trim Delete string ${"string"? Trim}à result is string

Iv. Operating figures

1. c is used to convert a number to a string

${123?c}à result is 123

2. String used to convert a number to a string

Three number formats were booked in Freemarker: number,currency (currency) and percent (percent) where number is the default numeric format conversion

For example:

< #assign tempnum=20>

${tempnum}

${tempnum?string.number} or ${tempnum?string ("number")}à result of 20

${tempnum?string.currency} or ${tempnum?string ("currency")}à result is ¥20.00

${tempnum?string. Percent} or ${tempnum?string ("percent")}à result 2,000%

Five, Operation Boolean value

string is used to convert a Boolean value to a string output
True to "true", false to "false"
Foo?string ("Yes", "no") if the Boolean value is True, then return "yes", otherwise return no

Freemarker built-in function usage notes

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.