Getting started with freemarker Template Development, freemarker Template

Source: Internet
Author: User

Getting started with freemarker Template Development, freemarker Template

Data Model

Scalars scalar: Specify its path from the root, separated by vertices.

For example: whatnot. fruits

Sequences sequence: Use square brackets of an array to access the subvariables of a sequence.

For example, animals [0]. name, whatnot. fruits [1]

Summary:

Data models can be viewed as tree structures.
A scalar stores a single value. This type of value can be a string, number, date/time, or Boolean value.
A hash table is a container that stores variables and related variables with unique names.
A sequence is a container that stores ordered variables. The stored variables can be retrieved through digital indexes, which usually start from scratch.


Template

$ {...} : FreeMarker will output real values to replace the expressions in curly brackets. Such expressions are called interpolations interpolation.

FTL tags (the language tags of the FreeMarker template): They are FreeMarker instructions and cannot be directly output. These labels generally start with a symbol. (The User-Defined FTL tag is replaced by the @ symbol #)

Comments: the Comments of FreeMarker are similar to those of HTML, but they are separated by <# -- and -->. Any content between these two delimiters (including the delimiters) will be ignored by FreeMarker and will not be output.

Directives command: indicates the FTL tag. These commands have the same relationship with HTML tags (such as <table> and </table>) and HTML elements (such as table elements. (If you cannot distinguish them, you can regard "FTL tag" and "Instruction" as synonyms .)

Command example

1 if command

Let's take a closer look at the use of condition (condition judgment): = is an operator used to determine equal values on both sides of a condition. The comparison result is a Boolean value, true, or false. On the left side of = is the referenced variable. We are very familiar with this syntax and it will be replaced by the variable value. The right side is the specified string, and the string in the template must be placed in quotation marks.
Use the <# else> label to specify the program execution content when the condition is false.

<#if animals.python.price < animals.elephant.price>  Pythons are cheaper than elephants today. <#else>  Pythons are not cheaper than elephants today. </#if> 
If the variable itself is a Boolean value (true or false), you can directly use it as the if condition.
Condition:
<#if animals.python.protected>  Warning! Pythons are protected animals! </#if> 

2 list Command
When you need to use a list to traverse the content of a set, the list command is very useful. For example, if you use the preceding example to describe the data model of the sequence in the template.

<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> 
The general format of the list command is:
<# List sequence as loopVariable> repeatThis </# list>
The repeatThis part repeats the given sequence in each item, starting from the first item, one after another. Among all the duplicates, loopVariable will hold the value of the current item. This loop variable only exists in <# list…> And </# list> labels.

3 include command

Using the include command, We can insert the content of other files in the current template.
Suppose you want to display the copyright notice information on some pages. You can create a file to separately include the copyright notice, and then insert it where you need it. In other words, we can save the copyright information form to the page file copyright_footer.html.

You can use the include command to insert a file.


4. Combined instructions

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

5. Process nonexistent Variables

You can specify a default value wherever variables are referenced to avoid loss of variables! And default value. As in the following example, when a user is lost from the data model, the template will represent the user value as a string "Anonymous ". (If the user is not lost, the template will show
The output "Anonymous" does not exist ):

Of course, you can also place it after the variable name ?? To check whether a variable of FreeMarker exists. Merge it with the if command. if the user variable does not exist, the entire greeting code segment will be ignored:

<#if user??>For multi-level access variables, such as animals. python. price, write the code: animals. python. price! 0. Only when animals. python exists, and only the price of the last sub-variable may not exist (in this case, we assume the price is 0 ). If animals or python does not exist, the template processing will stop with the "undefined variable" error. To prevent this, you can write code (animals. python. price) like this )! 0. In this case, when animals or

If python does not exist, the expression returns 0. For ?? It is also used to process this logic:
Animals. python. price ?? Comparison (animals. python. price )?? Let's see.









Freemarker beginners

The freemarker needs to load the template file and then combine it with the data to generate HTML

Freemarker sets the template path. The question is answered clearly.

Reqeust. getRealPath ("/") + "ftl"

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.