FreeMarker数据基础
2010-07-02 09:55:02| 分类: FreeMarker 2.3.1 | 标签: |字号大中小 订阅
1.数据两种表示(在${}中):
例如下:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() |
第一种表示方法:whatnot.fruits[1](表示的是fruits下的值为banana的变量)。
第二种表示方法:直接用点号分隔到属性-----animals[0].size(表示animals下的第一个部分中的size属性,其值为small)。
2.FreeMarker中的三种类型:
${...}:输出时,FreeMarker将会用确定的值代替它。
FTL标签:在FreeMarker中使用#开头。
注释:在FreeMarker中注释方法为<#-- -->。
例1:
简单的判断:
<html>
<head>
<title>Welcome!</title>
</head>
<body>
<h1>
Welcome ${user}<#if user == "Big Joe">, our beloved leader</#if>!
</h1>
<p>Our latest product:
<a href="${latestProduct.url}">${latestProduct.name}</a>!
<#if animals.python.price < animals.elephant.price>
Pythons are cheaper than elephants today.
<#else>
Pythons are not cheaper than elephants today.
</#if>
</body>
</html>
例2:
列表list:
输入:
![]() | ![]() | ![]() |
![]() |
|
前台输出:
![]() | ![]() | ![]() |
![]() |
|
例3:
包含文件(include):
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() |
3.嵌套使用指令
例4:
输出列表并使size为"larger"的输出font比其他的大:
| ![]() | |
![]() |
4.两种处理变量丢失方法。
FreeMarker不能容忍变量丢失,除非事先指示如果丢失怎么处理
(!注意:在FreeMarker中不存在变量和变量的值为null是等价的,所以这里这两种情况都适用)
第一种方式:
![]() | ![]() | ![]() |
![]() |
|
第二种方式:
![]() | ![]() | ![]() |
![]() |
|
(!注意:如果写成这样animals.python.price!0,只有animals.python 没有丢失才正确,
如果animals或者python丢失,将会报"undefined variable"错误,这时,你可以写成(animals.python.price)!0的模式,可以避免这个问题)
(!注意:和上面一样animals.python.price??也应该写成(animals.python.price)??)
(!注意:在2.4版本及之前,${x!1 + y}将被误解为${x!(1 + y)} ,所以,应当写成${(x!1) + y}形式)
如果希望默认值为0或者false,可以写成如下形式:
| ![]() | |
![]() |
输出:
![]() |
| ![]() | |
![]() |
例5:
![]() |
| ![]() | |
![]() |
输出:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() |
5.FreeMarker中特殊变量及意义