Introduction to Freemarker syntax

Source: Internet
Author: User

FTL is a template markup language for rendering data and for entering HTML structures. The syntax is as follows:

${book.name}

${book.name?if_exists}//value exists

${book.name??}//value exists

${book.name?has_content}//value exists

${book.name?default ("xxx")}//Default value XXX

${book.name! " XXX "}//Default value XXX

${book.date?string ("Yyyy-mm-dd")}//Date format

< #setting datetime_format= "Yyyy-mm-dd"/> ${post.pubtime?number_to_datetime}//date format, type long converted to YYYY-MM-DD format

${book?string.number}//number format, 20

${book?string.currency}//$20.00

${book?string.percent}//20%

< #assign foo=true>//declaring variables
${foo?string ("Yes", "No")}//yes

Use Lt,gt,lte,gte to replace comparison symbols

Built-in functions: use? To replace eg.${book?html}
String:
HTML---HTML Encoding of strings
Cap_first---String first letter Capitalization
Lower_case---Convert a string to lowercase
Trim---Remove whitespace characters before and after characters

sequences (sequence):
${list?size} size---Gets the number of elements in the sequence

Logical judgment

< #if condition>....< #elseif condition2>...< #else >....</#if >

Quickly define a collection of int intervals
< #assign len = 0..100>//No need to use []

Looping through collections
< #list items as item> ${item} </#list >
Item_index: Index value of the current variable
Item_has_next: Whether the next object exists
< #if items?size! = 0></#if >: Judging the length of the set

Macros/Templates
Define macro:< #macro greet><p> Paragraph paragraph paragraphs <p></#macro >
Using the macro:< @greet ></@greet >
Define macro parameters: Follow the name of the macro
< #macro greet person color><p> Paragraph paragraph paragraphs <p></#macro >
When calling parameters, do not care about order issues:< @greet person= "John" color= "Red" ></@greet >
Use < #nested > nested macro Calls

Definition of hash and list
< #assign c={"a": "AAA", "B": "BBBB"}>
${C.A}//' AAA '

< #assign c=[1,2,3,4,5]>
< #list c[1..4] as v>
${V}
</#list >

Using compress to process output
< #compress >....</#compress > eliminate blank lines
< @compress single_line=true>...</@compress > compressed to one line

${} shows the,<> definition operator for the rendering of the variable? Function name

FTL instruction Rules:
1. Start tag < #if ...>
2. End tag </#if >
3. Empty Label < #assign x=2.582/>
The symbol at the front of the label is #, which refers to the system built-in instructions.
When the symbol at the front of the tag is @, it refers to the user instruction.

Reference: http://www.havenliu.com/goodarticle/420.html

Introduction to Freemarker syntax

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.