Freemarker Beginner's Guide

Source: Internet
Author: User
Tags arithmetic comments comparison hash logical operators

1. Cheat Sheet

String: "foo" or "foo" or "It s \" quoted\ "or R" C:\raw\string "
 Number: 123.45 < #list [BR] animals as[br] BEING[BR] >[br] ${being.name} for ${being.price} EUROS[BR] </#list >
 Boolean value: True, False
 sequence: ["foo", "Bar", 123.45], 1..100
 Hash Table: {"name": "Green Mouse", "Price": 150}
 Retrieving variables
 Top-level variable: User
 retrieving data from the hash table: User.Name, user["name"]
 retrieved from sequence: Products[5]
 Special variables:. Main
 String manipulation
 interpolation (or connection): "Hello ${user}!" (or "free" + "Marker")
 Get one character: Name[0]
 Sequence operation
 Connection: Users + ["Guest"]
 Sequence Segmentation: products[10..19] or products[5.
 Hash table operation
 Connection: Passwords + {"Joe": "Secret42"}
 Arithmetic operation: (x * 1.5 +)/2-y% 100
 Comparison operations: x = = y, x! = y, x < y, x > y, x >= y, x <= y, x &lt; Y, wait.
 Logic operation:!registered && (firstvisit | | fromeurope)
 Built-in functions: Name?upper_case
 Method invocation: Repeat ("what", 3)
 Handling non-existent values
 Default value: name! " Unknown "or (User.Name)!" Unknown "or name! or (User.Name)!
 detect non-existent value: Name?? or (User.Name)??

2. String manipulation

interpolation (or connection)

${...} ${"${user}${user}${user}${user}"}

${user[0]} equals user?string[0]


3. Sequence operations

< #list ["Joe", "Fred"] + ["Julia", "Kate"] as User>
-${user}
</#list >

Sequence segmentation

[FirstIndex. LASTINDEX]. If SEQ stores the sequence "A", "B", "C", "D", "E", "F", then the expression seq[1..4] will be a sequence that contains "B", "C", "D", "E", from Freemarker 2.3.3 after the lastindex can be omitted.


4. Hash table Operations

< #assign ages = {"Joe": Max, "Fred": + + {"Joe": +, "Julia":18}>
-Joe is ${ages. Joe}
-Fred is ${ages. Fred}
-Julia is ${ages. Julia}

Print

-Joe is 30
-Fred is 25
-Julia is 18


5. Arithmetic operations

Arithmetic operations consist of basic arithmetic and modulo operations, with the following operators:
 Addition: +
< #assign ages = {"Joe": Max, "Fred": + + {"Joe": +, "Julia":18}>
-Joe is ${ages. Joe}
-Fred is ${ages. Fred}
-Julia is ${ages. Julia}
-Joe is 30
-Fred is 25
-Julia is 18
 Subtraction:-
 Multiplication: *
 Division:/
 Modulus (redundancy):%

${100–X*X}
${X/2}
${12%10}

+ number, which can be used to concatenate strings of ${3 + "5"}--"Freemarker does not automatically convert the string to a number, but instead automatically

${(X/2)? int} integer that evaluates the result

Comparison operation

< #if user = "Big Joe" >
It is Big Joe
</#if >
< #if user! = "Big Joe" >
It's not Big Joe
</#if >

< #if (x > Y) > Equals < #if x &gt; Y>

Use LT instead of <,lte instead of <=,GT instead of >,gte instead of >=

Common logical operators:
 Logic or: | |
 Logic and:&&
 Logical non-:!


6. Built-in functions

Built-in functions used by strings:
html: All special HTML characters in a string need to be replaced with entity references (such as < instead of &lt;).
cap_first: The first letter of a string becomes uppercase
lower_case: Lowercase form of string
upper_case: Uppercase form of a string
trim: Remove whitespace from the end of a string
Built-in functions used by the sequence:
size: Number of elements in a sequence
Built-in functions for digital use:
int: The integer part of the number (e.g. -1.9?int is-1)

7. Method invocation

${repeat ("What", 3)} print Whatwhatwhat


8. Handle non-existent values Freemarker 2.3.7 later (used instead of the built-in functions default,exists and if_exists).

This will appear undefinded

9. Default value

Unsafe_expr!default_expr or unsafe_expr! or (unsafe_expr)!default_expr or (unsafe_expr)!
This operator allows you to specify a default value or null empty sequence empty hash for a variable that may not exist.

Recommended Use (UNSAFE_EXPR)!default_expr or (unsafe_expr)! No matter how many layers there are, errors change to default values

Allows any part of an expression to be undefined

such as (Something.product.color)! " Red "Something is null and defaults to red

10. Detecting non-existent values

Unsafe_expr?? or (unsafe_expr)??


11. Operator precedence rules with Java JavaScript


12. Interpolation can only be used in two places:

Text areas (such as

If the interpolation is in the text area (that is, no longer in the string expression), the string that will be inserted is automatically escaped if the Escapse directive works. If you are generating HTML, it is highly recommended that you use it to block cross-site scripting attacks and non-well-formed HTML pages.

< #escape x as x?html>
...
<p>title: ${book.title}</p>
<p>description: < #noescape >${book.description}</#noescape ></p>
< #list comments as Comment>
<div class= "comment" >
${comment}
</div>
</#list >
...
</#escape >

American digital Format ${product.id?c} for the machine to see

Date/Time Insertion guide

The setting of Date_format,time_format and Datetime_format

Boolean Insertion Guide

Error after ${married?string ("Yes", "No")} ${a = = 2}

Precise conversion rules
For those interested in the study, the value of the expression is converted to a character (which still does not exist) the exact rules of the string are the following, in this order:
1. If the value is a number, it is converted to a string according to the specified Number_format setting rule. So these conversions are usually made to the user, not to the computer.
2. If this value is one of the date, time, or time date types, then they are converted to strings according to the specified Time_format,date_format and Datetime_format setting rules, depending on whether the date information contains only the time, date, or all. If it cannot be detected which date type (date or time or datetime), an error occurs.
3. If the value is originally a string type, no conversion is required.
4. If the Freemarker engine is in traditional compatibility mode:
1. If the value is a Boolean type, then it is converted to "true" and the false value will be converted to an empty string.
2. If the expression is not defined (null or the variable is undefined), then it is converted to an empty string.
3. Otherwise, an error abort template execution occurs.
5. Otherwise, an error abort template execution occurs.


Index of the sequence

< #if (secondGoodsCategoryT3??) && (secondgoodscategoryt3?size>0) >
< #list secondGoodsCategoryT3 as second3>

< #if Second3_index = = 0>
<a href= "${basepath}/${second3.htmlpath}}" target= "_blank" >${second3.name}</a>

</#if >
< #if second3_has_next>
|
</#if >
</#list >
</#if >

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.