FreeMarker Design Guide (1)
1, get started
(1) Template Data Model = Output
l FreeMarker is based on designers and programmers are the concepts of different individuals with different professional skills.
l They are division of labor: designers focus - create HTML files, pictures, other visualizations of the web page; programmers create the system, generate the data to display by the design page
l The problem that is often encountered is: Information displayed in the web page (or other type of document) is invalid when the design page is based on dynamic data.
l Here, you can add some specific instructions in HTML (or other you want to output), and FreeMarker will replace these code with the appropriate data when the output page gives the end user.
l The following is an example:
hEAD>
Our Latest Product:
body>
html>
l This example is a specific code surrounded by a simple HTML. These specific code is a directive for FreeMarker, and a file containing the freemarker is called a template (Template)
l As for User, LatestProduct.URL and LatestProduct.name from Data Model (Data Model)
l The data model is created by programmer, providing changes to the template, which comes from the database, file, and even directly generates in the program.
l Template designer doesn't care about data from there, I only know that the use of data models has been used.
l The following is a possible data model:
(root)
|
- user = "big joe"
|
- LatestProduct
|
- URL = "Products / Greenmouse.html"
|
- Name = "Green Mouse"
l The data model is similar to the computer's file system.
l When FreeMarker combines the above data model into the template, the following output is created:
hEAD>
Our Latest Product:
body>
html> (2) Data model
l The typical data model is a tree structure, which can be arbitrarily complex and deep, as described below:
(root)
|
- Animals
| | |
| - MOUSE
| | | | |
| | - SIZE = "small"
| | | | |
| | - Price = 50
| | |
| - ELEPHANT
| | | | |
| | - SIZE = "Large"
| | | | |
| | - Price = 5000
| | |
| - Python
| | |
| - SIZE = "Medium"
| | |
| - price = 4999
|
- Test = "IT IS A TEST"
|
- WhatNOT
|
- Because = "don't know"
l Similar to the variable of the directory is called HASHES, including the only query name that saves subordinate variables
l Similar to file variables are called Scalars, save single values
l Scalars has two types: strings (enclosed with quotation marks, can be single or double quotes) and numbers (do not use quotes to enclose the numbers, this will be handled as a string)
l Starting from root from root, all parts are separated from root, such as Animals.Mouse.price.
l The other variable is SEQUENCES, and HASHES, but does not use the variable name, and the digital index is used, as described below:
(root)
|
- Animals
| | |
| - (1st)
| | | | |
| | - Name = "mouse"
| | | | |
| | - SIZE = "small"
| | | | |
| | - Price = 50
| | |
| - (2nd)
| | | | |
| | - Name = "ELEPHANT"
| | | | |
| | - SIZE = "Large"
| | | | |
| | - Price = 5000
| | |
| - (3RD)
| | |
| - Name = "python"
| | |
| - SIZE = "Medium"
| | |
| - price = 4999
|
- WhatNOT
|
- fruits
|
- (1st) = "Orange"
|
- (2nd) = "banana"
l This use index for SCALARS, such as Animals [0] .name
(3) Template L can include the following three specific portions in the FreeMarker template:
Ø {...}: It is called Interpolations, freeMarker is replaced with actual values when output
Ø FTL tag (FreeMarker Template Language Tag): Similar to the HTML tag, in order to distinguish between the HTML mark, use # start (starting with @, in the following description)
Ø Note: Between <# - and -> (not )
l The following is some examples of use instructions:
Ø IF instruction
<#if animals.python.price
Pythons Are Cheace Than Elephants Today.
<#else>
Pythons Are Not Check Than Elephants Today.
# if>
Ø LIST instruction
we have eachse animals: