Common Java template engine freemarker labels (1)

Source: Internet
Author: User

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 are> symbols in it, becauseFreemarkerThe> symbol is treated as the end character of the tag, which may lead to program errors. To avoid this situation, we should use brackets wherever these symbols appear.

<#if animals.python.price < animals.elephant.price>
  Pythons are cheaper than elephants today.
<#else>
  Pythons are not cheaper than elephants today.
</#if> 

 
2. Switch, Case, default, and break commands

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>
3. List and break commands

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>

<p>We have these animals:
<table border=1>
  <tr><th>Name<th>Price
  <#list animals as being>
  <tr><td>${being.name}<td>${being.price} Euros
  </#list>
</table>  

Output:

<p>We have these animals:
<table border=1>
  <tr><th>Name<th>Price
  <tr><td>mouse<td>50 Euros
  <tr><td>elephant<td>5000 Euros
  <tr><td>python<td>4999 Euros
</table>  

 
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.

  <title>Test page</title>
<body>
  
  <p>Blah blah...
<#include "/copyright_footer.html">
</body>
5. Import command

This command is used to importFreemarkerAll variables in the template are placed in the specified map object. The syntax format of the import command is as follows:
<# Import "/lib/common. FTL" As COM>
The above code imports all the variables in the/lib/common. FTL template file and places these variables in a map object named COM.

  1. Create a database

The following is an example of creating a database (Suppose it is saved in lib/my_test.ftl ):

<#macro copyright date>
  <p>Copyright (C) ${date} Julia Smith. All rights reserved.
  <br>Email: ${mail}</p>
</#macro>  
<#assign mail = "jsmith@acme.com"> 

Use the import command to import data to the template,FreemarkerA new namespace will be created for the imported database, and the variables in the database can be accessed through the hash variables specified in the import command:

<#import "/lib/my_test.ftl" as my>
<#assign mail="fred@acme.com">
<@my.copyrightdate="1999-2002"/>
${my.mail}
${mail}  

Output result:

  <p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.
  <br>Email: jsmith@acme.com</p>
jsmith@acme.com
fred@acme.com  

We can see that the two variables with the same name in the example do not conflict because they are located in different namespaces.
L you can use the assign command to create or replace variables in the imported namespace. The following is an example:

<#import "/lib/my_test.ftl" as my>
${my.mail}
<#assign mail="jsmith@other.com" in my>
${my.mail}  

L output result:

jsmith@acme.com
jsmith@other.com  

L the variables in the data model are visible anywhere and contain different namespaces. The following is the modified Library:

<#macro copyright date>
  <p>Copyright (C) ${date} ${user}. All rights reserved.</p>
</#macro>
<#assign mail = "${user}@acme.com">   

L assume that the value of the user variable in the data model is Fred, the following code:

<#import "/lib/my_test.ftl" as my>
<@my.copyright date="1999-2002"/>
${my.mail}   

L output result:

  <p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>

Fred@acme.com

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.