Freemarker Common commands

Source: Internet
Author: User

 

FreeMarker's FTL commands are also an important part of the template. These commands can facilitate iteration and branch control of data contained in the data model. in addition, some important functions are also implemented through the FTL command.

4.1 if command

This is a typical Branch Control Command. Its function is similar to if in Java. The syntax format of the if command is as follows:
<# If condition>...
<# Elseif condition>...
<# Elseif condition>...
<# Else>...
</# If>

Example:
<# Assign age = 23>
<# If (age> 60)> elderly
<# Elseif (age> 40)> middle-aged
<# Elseif (age> 20)> young people
<# Else> young man
</# If>
Output: young people
The logic expression in the above Code is enclosed in parentheses mainly because there is a> symbol in it. Because FreeMarker regards> as the end character of the tag, it may cause program errors. To avoid this situation, we should use brackets wherever these symbols appear.

4.2 switch, case, default, break command

These commands are obviously branch commands, which act similar to the switch statement of Java. The syntax structure of the switch command is as follows:
<# Switch value>
<# Case refValue>... <# break>
<# Case refValue>... <# break>
<# Default>...
</# Switch>

4.3 list, break command

THE list command is an iterative output Command Used To iterate the set of output data models. The syntax format of the list command is as follows:
<# List sequence as item>
...
</# List>
In the preceding syntax format, sequence is a collection object or expression. However, this expression returns a collection object, and item is an arbitrary name, is the set element output by iteration. in addition, when the set object is iterated, there are two special cycle variables:
Item_index: index value of the current variable
Item_has_next: whether the next object exists
You can also use the <# break> command to exit iteration.

Example:
<# List ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] as x>
$ {X_index + 1}. $ {x} <# if x_has_next>, </if>
<# If x = "Thursday"> <# break> </# if>
</# List>

4.4 include command

The include command is similar to the JSP containing command. The syntax format for containing the specified page. include command is as follows:
<# Include filename [options]>
In the preceding syntax format, the two parameters are described as follows:
Filename: Specifies the template file to be included.
Options: this parameter can be omitted. It specifies the included options, including encoding and parse options. encoding specifies the decoding set used when the page is contained, parse specifies whether the included file is parsed as an FTL file. If the parse option value is omitted, the default value of this option is true.

4.5 import command

This command is used to import all variables in the FreeMarker template and place the variables in the specified Map object. The syntax format of the import command is as follows:
<# Import "/lib/common. ftl" as com>
The code above will import all the variables in the/lib/common. ftl template file and place these variables in a Map object named com.

4.6 noparse command

The noparse command specifies that FreeMarker does not process the content contained in the specified command. The syntax format of this command is as follows:
<# Noparse>... </# noparse>

See the following example:
<# Noparse>
<# List books as book>
<Tr> <td >$ {book. name} <td> author: $ {book. author}
</# List>
</# Noparse>
The output is as follows:
<# List books as book>
<Tr> <td >$ {book. name} <td> author: $ {book. author}
</# List>

4.7 escape and noescape commands

The escape command automatically adds the escape expression to the interpolation in the body area, but does not affect the interpolation in the string. It only affects the interpolation in the body. The syntax format of the escape command is as follows:
<# Escape identifier as expression>...
<# Noescape>... </# noescape>
</# Escape>

See the following code:
<# Escape X as X? HTML>
First name: $ {firstname}
Last name: $ {lastname}
Maiden name: $ {maidenname}
</# Escape>
The above code is equivalent:
First name: $ {firstname? HTML}
Last name: $ {lastname? HTML}
Maiden name: $ {maidenname? HTML}

The escape command works when parsing the template rather than when running. In addition, the escape command is also nested and used. The child escape inherits the rules of the parent escape, as shown in the following example:
<# Escape X as X? HTML>
Customer name: $ {customername}
Items to ship;
<# Escape X as itemcodetonamemap [x]>
$ {Itemcode1}
$ {Itemcode2}
$ {ItemCode3}
$ {ItemCode4}
</# Escape>
</# Escape>
The above code is similar:
Customer Name: $ {customerName? Html}
Items to ship;
$ {ItemCodeToNameMap [itemCode1]? Html}
$ {ItemCodeToNameMap [itemCode2]? Html}
$ {ItemCodeToNameMap [itemCode3]? Html}
$ {ItemCodeToNameMap [itemCode4]? Html}

For all interpolation in the escape command, this interpolation will be automatically added with an escape expression. If you need to specify some interpolation in the escape command, you should use the noescape command, the interpolation in the noescape command does not add an escape expression.

4.8 assign command

The assign command has been used multiple times before. It is used to create or replace a top-level variable for the template page. The assign command is used in many ways, including creating or replacing a top-level variable, you can also create or replace multiple variables. Its simplest syntax is as follows: <# assign name = value [in namespacehash]>, which is used to specify a variable named name, the value of this variable is value. in addition, FreeMarker allows you to add an in clause to the assign command. The in clause is used to place the created name variable in the namespacehash namespace.

The assign command also has the following usage: <# assign name1 = value1 name2 = value2... nameN = valueN [in namespacehash]>. This syntax can create or replace multiple top-level variables at the same time. in addition, it has a complicated usage, if the variable value to be created or replaced is a complex expression, you can use the following syntax format: <# assign name [in namespacehash]> capture this </# assign>, in this syntax, the content of the assign command is assigned to the name variable. example:
<# Assign x>
<# List ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] as n>
$ {N}
</# List>
</# Assign>
$ {X}
The above code will generate the following output: Monday Tuesday Thursday Friday Saturday

Although assign specifies the usage of this complex variable value, we should not abuse this usage, as shown in the following example: <# assign x> Hello $ {user }! </# Assign>. The above code is better written as follows: <# assign x = "Hello $ {user }! ">

4.9 setting command

This command is used to set the runtime environment of FreeMarker. The syntax format of this command is as follows: <# setting name = value>. in this format, the value range of name includes the following:
Locale: Specifies the country/language option used by the template.
Number_format: Format of the output number.
Boolean_format: Specifies the syntax format of two boolean values. The default value is true and false.
Date_format, time_format, datetime_format: Format of the output date.
Time_zone: specifies the time zone used to format the output date.

4.10 macro, nested, return command

Macro can be used to implement custom commands. By using custom commands, you can define a template segment as a user instruction. The syntax format of macro commands is as follows:
<# Macro name param1 param2... paramN>
...
<# Nested loopvar1, loopvar2,..., loopvarN>
...
<# Return>
...
</# Macro>
The preceding format snippet contains the following parts:
The name: name attribute specifies the name of the custom command. Multiple parameters can be input when using the custom command.
ParamX: This attribute is used to specify parameters when using custom commands. When using this custom command, you must input values for these parameters.
Nested command: The intermediate part of the nested label output when using custom commands
Loop variable in the nested command: this loop variable will be specified by the macro definition part and passed to the template using the tag
Return command: This command can be used to end the custom command at any time.

See the following example:
<# Macro book> // define a custom instruction
J2ee
</# Macro>
<@ Book/> // use the command just defined
The output result of the above Code is: j2ee

In the above Code, it may be difficult to see the use of custom tags, because the content of the book commands we define is very simple. In fact, custom tags can contain a lot of content, this allows for better code reuse. in addition, you can specify parameters for custom commands when defining custom commands. See the following code:
<# Macro book booklist> // define a custom instruction. booklist is a parameter.
<# List booklist as book>
$ {Book}
</# List>
</# Macro>
<@ Book booklist = ["spring", "j2ee"]/> // use the command just defined
The above code passes in a parameter value for the book command. The output result of the above Code is: spring j2ee

In addition, you can use the nested command to output the intermediate part of the custom command when using the custom command. See the following example:
<# Macro page title>
<Html>
<Head>
<Title> FreeMarker Example page-$ {title? Html} </title>
</Head>
<Body>
<H1 >$ {title? Html} <# Nested> // The label body used to introduce custom commands.
</Body>
</Html>
</# Macro>
The code above defines an HTML page template as a page instruction, so the page instruction can be performed on other pages:
<# Import "/common. ftl" as com> // assume that the preceding template page is named common. ftl. The import page
<@ Com. page title = "book list">
<U1>
<Li> spring </li>
<Li> j2ee </li>
</Ul>
</@ Com. page>

From the above example, we can see that using macro and nested commands can easily achieve the page decoration effect. In addition, you can also specify one or more cycle variables when using the nested command, see the following code:
<# Macro book>
<# Nested 1> // a loop variable value is specified when the book command is used.
<# Nested 2>
</# Macro>
<@ Book; x >$ {x}. Books </@ book>
When using the nested command to pass in the variable value, when using the custom command, you need to use a placeholder (such as the; x after the book command). The above code output text is as follows:
1. Books 2. Books

When using cyclic variables in the nested command, you can use multiple cyclic variables. See the following code:
<# Macro repeat count>
<# List 1 .. count as x> // Three Cyclic variables are specified when the nested command is used.
<# Nested x, x/2, x = count>
</# List>
</# Macro>
<@ Repeat count = 4; c halfc last>
$ {C}. $ {halfc} <# if last> Last! </# If>
</@ Repeat>
The above output result is:
1. 0.5 2. 1 3. 1.5 4. 2 Last;

The return command ends the macro command. Once the return command is executed in the macro command, FreeMarker does not continue to process the content in the macro command. See the following code:
<# Macro book>
Spring
<# Return>
J2ee
</# Macro>
<@ Book/>
The above code output is spring, but j2ee is not output after the return command.

Related Article

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.