Freemark Applications and Benefits

Source: Internet
Author: User

In B/s programming, there are two artists and programmers who have different professional skills: artists focus on performance-creating visual elements such as pages, styles, layouts, effects, and so on, while programmers are busy creating business processes for programs, generating data to be displayed on design pages, and so on. Most of the time, the data to be displayed does not exist at design time, and they are usually generated by the program at run time, such as the return result of the "USB Disk with a price not higher than 800NT" query. This technical requirement has produced scriptlet,jsp such as JSP, which is very powerful, but is often abused and leads to some undesirable consequences.

Mix logic and performance together.
Destroys the normal decomposition of the artwork and programmer's responsibilities.
Make JSP pages difficult to read and maintain.
The template engine was created to solve the problem above. When designing HTML, we add specific instructions to specify which data to insert, the HTML or other text with special instructions, which we call a template. The template engine replaces the code with the appropriate data when it prints the page.
Templates and embedded JSP HTML are different, template directives have limited programming ability, you can avoid mixing business logic.
30,000 feet overlooking Freemarker
Simply put, Freemarker is a template engine written in Java that outputs multiple specifications of text based on the template. In particular, Freemarker is not related to the Web application framework, it can also be applied in a non-web application environment.

Let's take a look at Freemarker's template: (PRODUCT.FTL)



<title>Welcome!</title>


<body>


<p>our Latest Product:

<a href= "${latestproduct.url}"
>${latestproduct.name}</a>!

</body>

This example is added in simple HTML by ${...} Surrounded by specific code, these specific code is freemarker instructions.

The specific content of user, Latestproduct.url, and Latestproduct.name is derived from the data model. The data model is programmed by programmers to provide information about changes to the template, which is generated from databases, files, and even directly in the program.

The template designer doesn't care where the data comes from, but only the data model that has been built.

Run Freemarker with FMPP (Freemarker preprocessor)
The first explanation is that Freemarker's operation is not dependent on fmpp. Fmpp is just a freemarker auxiliary tool, with it, we can quickly debug Freemarker output results, without the use of Java programming, which can greatly reduce the design of the designers to debug difficult.

To create a related folder on disk:

D:/work/src/product.ftl

d:/work/out/

D:/work/data/product.tdd

D:/work/config.fmpp
The configuration file (CONFIG.FMPP) that we use is set as follows:

Sourceroot:src

Outputroot:out

LogFile:log.fmpp

modes: [

Copy (Common/**/*.*, resource/*.*)

Execute (*.FTL)

Ignore (Templates/*.*,. Project, **/*.xml, Xml/*.*, *.js)

]

Replaceextensions: [FTL, HTML]

sourceencoding:gb2312

DATA:TDD (.. /DATA/PRODUCT.TDD)
Note: "DATA:TDD (.. /DATA/PRODUCT.TDD) "Specifies the data source for the template, TDD is one of the data formats supported by FMPP, see the FMPP documentation for TDD introduction, or refer to TDD. The PRODUCT.TDD content is this:

{

User: "Big Joe"


Latestproduct: {url: "products/greenmouse.html"
, Name: "Green Mouse"
}

}
Now execute under DOS (assuming FMPP is installed under D:/FMPP):

D:/work/>d:/fmpp/bin/fmpp
The final output is this, stored in the file out/product.html:



<title>Welcome!</title>


<body>


<p>our Latest Product:

<a href= "products/greenmouse.html"
>green mouse</a>!

</body>

As described in the Freemarker documentation, Freemarker works by:

Template + data = output!

Freemarker is not limited to generating HTML, it can even generate Java code, just depends on how you design the template.

Now with the powerful tool of FMPP, we can then quickly learn about the Freemarker instructions. Let us go!

The three basic object types used in the Freemarker template are: scalars, hashes, and sequences. Before interpreting these object types, let's look at the data model.

A typical data model is a tree structure that can have arbitrary deep layers, such as:

(Root)

|

+-Animals

| |

| +-Mouse

|   | |

|   | +-size = "small"


|   | |

|   | +-Price = 50

| |

| +-Elephant

|   | |

|   | +-size = "large"


|   | |

|   | +-Price = 5000

| |

| +-Python

| |

| +-size = "Medium"


| |

| +-Price = 4999
Each leaf in this tree is called scalars in Freemarker and is used to store a single value. Scalars holds two types of values: strings (enclosed in quotation marks, can be single or double quotes), numbers (do not enclose numbers in quotation marks, which are treated as strings), dates, and Boolean values. Access to scalars begins with Root, with "." For each part. Separate, as Animals.mouse.price.

Each branch of the tree is associated with a unique query name, such as "Mouse", "elephant", which acts as a container for other objects (Size,price), which is called hashes and references the TDD definition of hashes.

The sequences function is similar to hashes, and can also serve as a container for other objects, except that the variable name is used instead of the numeric index:

(Root)

|

+-Animals

| |

| +-(1st)

|   | |

|   | +-name = "Mouse"


|   | |

|   | +-size = "small"


|   | |

|   | +-Price = 50

| |

| +-(2nd)

|   | |

|   | +-name = "Elephant"


|   | |

|   | +-size = "large"


|   | |

|   | +-Price = 5000

| |

| +-(3rd)

| |

| +-name = "Python"


| |

| +-size = "Medium"


| |

| +-Price = 4999
The corresponding scalars can be accessed through animals[0].name. TDD definitions for reference sequences

For operations on the above three object types, see the various actions of the object type

Templates and Directives
In addition to the relevant text, the following three specific sections can be included in the Freemarker template:

${...} : called interpolation (interpolations), the Freemarker is replaced with the actual value at output.
directive: Also called Freemarker tag, similar to HTML tag, but starts with # (some start with @, described later).
Note: Contains text between <#--and, rather than <!--and--.
Control instructions
If directive
The IF directive, like most programming languages, also supports < #else if. Don't repeat it.

< #if
Animals.python.price < animals.elephant.price>

Pythons is cheaper than elephants today.

< #else
>

Pythons is not cheaper than elephants today.

</#if
>
List instructions
The list instruction iterates through every element in the sequences. The list directive has two hidden special variables:

Item_index the variable returns the index value of the element in the sequences.
Item_has_next the variable is a Boolean, false indicates that the element is the last element in the sequences.
<p>we have these animals:

<table border=1>

<tr><th>id<th>name<th>price

< #list animals as being>

<tr><td>${being_index+1}<td>${being.name}<td>${being.price} Euros

</#list >

</table>
The above template can print out the names and prices of three animals in turn, and Being_index and Being_has_next are special variables.

You can end the list loop early with the < #break > command.

Switch directives
Similar to switch statements in other languages.

< #switch
Being.size>

< #case
"Small"
>

This would be processed if
It is small

< #break
>

< #case
"Medium"
>

This would be processed if
It is medium

< #break
>

< #case
"Large"
>

This would be processed if
It is large

< #break
>

< #default
>

This would be processed if
It is neither

</#switch
>
Precautions
The FTL is case-sensitive, so the list is the correct FTL instruction, and list is not; ${name} and ${name} are different
Interpolation can only be used in text
The FTL tag cannot be located inside another FTL tag.
Annotations can be located inside the FTL tag and interpolation.
Extra whitespace characters are removed when the template is output
Available [#if:] To replace < #if. Avoid confusion with HTML tags.

Freemark Applications and Benefits

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.