The use of Freemarker, transferred from

Source: Internet
Author: User
Tags hash scalar
Freemarker (i)(2011-08-16-16:28:53)
Tags: topics Category: Learning

1. Template + data Model = Output

2, MVC pattern: model-view-controller abbreviation, Chinese translation is "model-view-controller".

3. Hash table access to other storage space through the name you can find

4, the array of square brackets to access a sequence of child variables: animals[0].name;whatnot.fruits[1], the index is usually zero-based.

5, the scalar storage of a single value, this type of value can be a string, number, date/time or Boolean value.

6, ${...} : Freemarker will output a true value to replace the expression within the curly braces.

FTL Tags Tags (language tags for freemarker templates): The use of these tags generally begins with a symbol #. (User-defined FTL tags use the @ symbol instead of #).

Comments Note : Freemarker comments are similar to HTML annotations, but they are separated by <#--and-->.

The directives instruction is the FTL label. This directive has the same relationship in the HTML tags (such as the table element), such as the <table> and </table> elements

7, using the if instruction can conditionally skip a part of the template,

Our esteemed leader is the only user variable value in the IF condition, which is displayed when it is the same as "big Joe." When the condition evaluates to False (Boolean), the content between the < #if condition> and </> tags will be skipped.

8. Use < #else > tags to specify what the program can do when the condition is false.

9. The general format of the list instruction is:< #list sequence as loopvariable>repeatthis</#list >

sequence: sequence; Repeatthis: Repeats in each item at the given sequence traversal, starting with the first item, one after the other.

10,include directives : Insert the contents of other files into the current template.  

Include a URL that shows the entire contents of the URL.

11, the processing does not exist the variable

By placing?? Ask Freemarker if a variable exists after the variable name. Merge it with the if instruction, then the entire greeting snippet will be ignored if the user variable does not exist

12, numerical

Values that do not need to be stored are also called values, for example: 100

When the template is executed, the temporary result of the calculation is also called a value, such as 20,120 (it will print 120)

14, supported types are: scalar: string; number; boolean; date

Container: Hash table, sequence, set

Subroutines: Methods and functions; user-defined Directives

Other/Rarely used: node

15, methods and functions:

A method variable avg is placed in the data model, averaging:

16, user custom directive:

Suppose you now have a variable, box, whose value is a user-defined instruction that prints some specific HTML information that defines a title and the information in it.

17, if it can be implemented, please use a custom instruction instead of a function/method

①, output (return value) is the mark (Html,xml, etc.). The main reason is that the return result of the function can be automatically escaped by XML (this is because the ${...} , and the output of a user-defined instruction is not (this is due to the characteristics of the <@...>, its output is assumed to be markup and therefore is no longer escaped).

②, side effects are important, not return values. For example, the purpose of an instruction is to add an entry to the server log. (In fact, you can't get the return value of a custom instruction, but some types of feedback are possible to set non-local variables)

③, which controls the process (like the list or if instruction), but cannot do so on the function/method.

18, Node

The primary role of a node is topology information, and it can also store node names, types (strings), Namespaces (strings).

19, the overall structure of the template (FTL is case-sensitive)

①, text text: text will be printed as is.

②, interpolation interpolation: This part of the output will be calculated to replace the value. Interpolation is delimited by ${and}

③, FTL tags Tags: very similar to HTML tags, but they are to give freemarker instructions and will not print in the output content.

④, comments annotations: Annotations are similar to HTML annotations, but are separated by <#--and-->.

Note: FTL is case-sensitive and interpolation can only be used in the middle of text (or in a string expression). FTL labels cannot be used in other FTL tags and interpolation. Annotations can be placed between the FTL label and the interpolation value.

20, the instructions of the call:

such as calling the list directive: Two tags are used syntactically:< #list animals as being> and </#list >.

Use @ To replace # for user-defined directives. such as:< @mydirective parameters>...</@mydirective >

21, cheat Sheet

㈠, specifying values directly

① string: ' foo ' or ' foo ' or ' It ' quoted\ ' or R ' C:\raw\string '

② Number: 123.45

③ 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 a character: Name[0]

㈣, Sequence operations

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: 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.