FreeMarker官网:http://freemarker.org/
本系列文章参考了FreeMarker官网手册,并转载以下文章:
http://blog.csdn.net/zhoubols/article/details/4870636
http://blog.csdn.net/zhoubols/article/details/4925932
http://blog.csdn.net/zhoubols/article/details/4925945
为了防止原问连接丢失,因此拷贝部分内容至此,用于参考,对原作者辛苦劳动表示感谢。
并希望读者能转至原文出处学习。
FreeMarker是模版标记语言(FreeMarker Templates Language,FTL);
模板(htm) + 数据模型(Hashes) = 输出(html)
FreeMarker模板中可以包括下面三种特定部分:
${name}:称为interpolations,FreeMarker会在输出时用实际值进行替代;
FTL标记(FreeMarker模板语言标记):类似于HTML标记,为了与HTML标记区分,用#开始,或@开始;
注释:包含在<#--和-->之间;
if指令
<#if person.age gte 1 >
成年人
<#else>
未成年人
</#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指令
<body>
<h1>Test page</h1>
<p>Blah blah...
<#include "/copyright_footer.html">
</body>
注意事项:
1、[BR]是用于换行的特殊字符序列;
2、FTL区分大小写,所以list是正确的FTL指令,而List不是;${name}和${NAME}是不同的;
3、Interpolation只能在文本中使用;
4、FTL标记不能位于另一个FTL标记内部,例如:
<#if <#include 'foo'>='bar'>...</if>
5、注释可以位于FTL标记和Interpolation内部,如下面的例子:
<h1>Welcome ${user <#-- The name of user -->}!</h1>[BR]
<p>We have these animals:[BR]
<ul>[BR]
<#list <#-- some comment... --> animals as <#-- again... --> being>[BR]
...
6、多余的空白字符会在模板输出时移除;
【未完待续。。。】