A Preliminary Study of freemarker

Source: Internet
Author: User

A Preliminary Study of freemarker

1. What is Freemarker?

Baidu Baike said: Freemarker is a template engine. I am a little white. Unfortunately, I do not know what the template engine is. I am not afraid to continue with the Baidu template engine, baidu Baike continued that the template engine (especially the template engine used for web development) is generated to separate user interfaces from business data and generate documents in specific formats, the template engine for websites generates a standard Html document. In my personal understanding, Freemarker is a template for generating standard HTML documents. Combined with Daniel's blog: "Freemarker is actually a simple web page display technology. To put it bluntly, it is a combination of Web Page Templates and data models. "Working Method:" The webpage template contains data embedded in the data model, Freemarker custom Process Control Language, Freemarker custom operation function, and so on. When loading the webpage, the Freemarker template automatically extracts data from the data model and explains the HTML page that the entire webpage is familiar." Okay, it's basically similar to what we understand. Now that you know what Freemarker is, let's take a look at Freemarker and learn it.

2. Freemarker basic knowledge

First, Freemarker is not a Web-developed application framework. It is a component applicable to the Web application framework and does not know the existence of HTTP protocol or Java servlet, it is only used to generate text content. as a component of the MVC framework view layer, it is used to provide solutions for Model2 application frameworks such as Struts.

Freemarker is in the form of template + Data Model = output. That is, Freemarker inserts some of its own command elements in the html file, which can make the static HTML page have some dynamic effects. The source code is as follows:

HTML page:

Welcome Big Joe! 

Our latest product: green mouse!

The Code modified by Freemarker:

 

Welcome $ {user }! 

Our latest product :$ {latestProduct. name }!

This template is stored on the Web server. When you access this page, Freemarker will intervene in the execution, dynamically convert the template, and replace $ {...} in the template with the latest data content {...}. It can be found that the file does not specify how to obtain values such as $ {...}, which is characteristic of Freemarker. As a component of the MVC framework view layer, its important idea is that the presentation logic is separated from the business logic. It doesn't care how to get the data. By default, the data has been obtained. The operations to get the data will be compiled by programmers and implemented by programming languages such as Java. The data used by the Freemarker template is packaged into a data-model data model. Therefore, the (HTML) template + data model = (Freemarker) Output

In addition, the basic structure of the data model is tree-like, which is illustrated in the Freemarker manual as a computer file system. I personally understand that, just like a class, it has attributes. For example, latestProduct has two attributes: url and name.

This figure is from the Freemarker Chinese manual. The article introduces: In this figure, variables play the directory role (root, animal, mouse, elephant, Python, whatnot), which is called a hash table. A hash table accesses other stored variables by a searchable name (such as "animal", "mouse", and "price. The following describes how to use these variables and write them in blue: only single-value variables (in the figure, size, price, text, and because) scalars scalar (including string, number, date/time, and boolean values). To use a sub-variable in the template, specify its path from the root, each level is separated by vertices. For example, the access price is written as animals. mouse. there is another form in the figure above price: In the figure above, such as animal, whatnot. fruits does not store the names of variables, but stores them in sequence. You can use digital indexes to access these variables. How to use this sequence: You can use square brackets of an array to access the subvariables of a sequence. Note that the index starts from 0. To get the name of the first animal, the code is animals [0]. name *************************************** *************** **************************************** **************************************** * ** Freemarker Basic Knowledge Part 2: template Introduction: The simplest template is an HTML file. When the client accesses the page, Freemarker needs to send HTML code to the client browser for display. If the page needs to be moved, it is necessary to place a special part in HTML that can be parsed by Freemarker. $ {...}: Interpolations previously introduced FTL tags: similar to HTML tags, but the Freemarker command does not output anything. The use of these tags generally starts with the symbol # (the User-Defined FTL tag is replaced by the @ symbol #) Comments note: the annotation of Freemarker is similar to that of HTML, however, use the <# -- and --> directives command: FTL tag (the relationship is similar to the HTML Tag table tag.
And
Description: if command: if it determines that the user is not Big Joe, it will skip the <# if condition> and the content between the labels. Other reference code: list command: the list traverses the content of the set. Sample Code: the output result will be like this: the general format of the list command is: <# list sequence as loopVariable> repeatThisinclude command: Use the include command, you can insert contents of other files in the current template. Example: display the copyright notice on some pages. You can create an exclusive copyright copyright_footer.html file and insert it where necessary. Include command insert: the output content is: (the example will traverse the animal collection and print the name of a large animal in a large font) to process non-existent variables: Freemarker cannot tolerate reference of non-existent variables, unless you explicitly tell it how to handle a variable that does not exist. (Note: non-existent variables and a null variable Freemarker cannot be tolerated.) There are two ways to deal with this problem: 1. You can specify the default variable name followed by it! And the default value. For example, if a user is lost, Anonymous 2 will be used by default. provide a solution to the loss in advance. For example, place question mark (?) after the variable name ?? To check whether a variable of Freemarker exists. If it does not exist, the entire code segment above will be ignored.

 

 

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.