freemarker 数据基础

FreeMarker数据基础

2010-07-02 09:55:02| 分类: FreeMarker 2.3.1 | 标签: |字号 订阅

1.数据两种表示(在${}中):

例如下:

(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"  

第一种表示方法: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:

输入:

 

<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>  

 

前台输出:

 

<p>We have these animals:  <table border=1>    <tr><th>Name<th>Price    <tr><td>mouse<td>50 Euros    <tr><td>elephant<td>5000 Euros    <tr><td>python<td>4999 Euros  </table>  

 

 

例3:

包含文件(include):

 

<html>  <head>    <title>Test page</title>  </head>  <body>    <h1>Test page</h1>    <p>Blah blah...  <#include "/copyright_footer.html">  </body>  </html>  

 

 

3.嵌套使用指令

例4:

输出列表并使size为"larger"的输出font比其他的大:

 

<p>We have these animals:  <table border=1>    <tr><th>Name<th>Price    <#list animals as being>    <tr>      <td>        <#if being.size == "large"><font size="+1"></#if>        ${being.name}        <#if being.size == "large"></font></#if>      <td>${being.price} Euros    </#list>  </table>  

 

 

 

4.两种处理变量丢失方法。

FreeMarker不能容忍变量丢失,除非事先指示如果丢失怎么处理

(!注意:在FreeMarker中不存在变量和变量的值为null是等价的,所以这里这两种情况都适用)

第一种方式:

 

<h1>Welcome ${user!"Anonymous"}!</h1>  

 

第二种方式:

 

<#if user??><h1>Welcome ${user}!</h1></#if>  

 

(!注意:如果写成这样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,可以写成如下形式:

 

(${mouse!})  <#assign mouse = "Jerry">  (${mouse!})  

 

输出:

 

()  (Jerry)  

 

例5:

 

<#if mouse??>    Mouse found  <#else>    No mouse found  </#if>  Creating mouse...  <#assign mouse = "Jerry">  <#if mouse??>    Mouse found  <#else>    No mouse found  </#if>  

 

输出:

 

  No mouse found  Creating mouse...    Mouse found  

 

5.FreeMarker中特殊变量及意义

官方地址:http://www.freemarker.org/docs/ref_specvar.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值