Getting Started with FreeMarker - Template + Data Model = Output

1. Output (take an HTML as an example) output

<html> 
<head> 
  <title> Welcome!</ title> 
</head> 
<body> 
  <h1> Welcome John Doe!</ h1> 
  <p>我们的最新产品:<a href =“ products / greenmouse.html “> 绿色老鼠 </a>!</p>
</ body> 
</ html>

2. Template Template

We need to get the data from the server, the template is to provide a sample, tell us how to get the data screen and display it on the template, and finally the data is an output result such as html

<html> 
<head> 
  <title> Welcome!</title> 
</head> 
<body> 
  <h1> Welcome${user}!</h1> 
  <p>我们的最新产品:
  <a href =“ ${latestProduct.url} “> $ {latestProduct.name} </a>!</p>
</body> 
</html>

From this we can see that when we access the template, FreeMarker will carefully replace the data in the corresponding position in the template through the ${...} method, and then display the output.

Notice:

The data to be displayed by this template is prepared outside of FreeMarker, usually written in some "real" programming language (such as JAVA). The role of the template is to keep the presentation problem and not involve transformation and logic problems. Therefore, it is not possible to update the data by changing the template, and it needs to be prepared outside FreeMarker.

3. Data Model Data Model

As far as templates are concerned, the data model is a tree structure. Similar to the file sister and file on the disk. We can visualize this as:

(root)

    | + - user = "Big Joe"

    | + - latestProduct

            | + - url =“products / greenmouse.html”

             | + - name="green mouse"

 

Notice:

The above is just a visualization; the data model is not in text format, but from Java objects. For a Java programmer, the root might be a Java object with getUser() and methods, or a Java with and keys. Again,  maybe Java objects  and  methodsgetLatestProduct()Map"user""latestProducts"latestProductgetUrl()getName()

In summary, FreeMarker needs a template and a data model to generate an HTML

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325059809&siteId=291194637