当前位置: 代码迷 >> 综合 >> Freemarker学习笔记(1)
  详细解决方案

Freemarker学习笔记(1)

热度:67   发布时间:2024-01-18 02:39:03.0

定义:

一个生成文本输出的模板引擎,但不是一个web框架(FreeMarker is a template engine: a generic tool to generate text output )

FreeMarker is not a Web application framework. It is suitable as a component in a Web application framework, but the FreeMarker engine itself knows nothing about HTTP or servlets. It simply generates text. As such, it is perfectly usable in non-web application environments as well. Note, however, that we provide out-of-the-box solutions for using FreeMarker as the view component of Model 2 frameworks such as Struts.

方法:Template + data model = output

DataModel:

(root)
|
+- animals(hashes)
|   |
|   +- mouse
|   |   |   
|   |   +- size = "small"( scalars)
|   |   |   
|   |   +- price = 50
|   |
|   +- elephant
|   |   |   
|   |   +- size = "large"
|   |   |   
|   |   +- price = 5000
+- whatnot
|
+- fruits(sequences)
|
+- (1st) = "orange"
|
+- (2nd) = "banana"

1。The variables that act as directories (the root, animals, mouse, ) are called hashes. Hashes store other subvariables by a lookup name.

2。The variables that store a single value (size, price) are called scalars.

3。similar to hashes, but they don't store names for the variables they contain

The template

FTL tags:

if-else 指令

<#if animals.python.price < animals.elephant.price>
Pythons are cheaper than elephants today.
<#else>
Pythons are not cheaper than elephants today.
</#if>  
list指令
  
   
<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>
include指令
注意:
区分大小写;指令不能嵌套;