freemarker常用标签

1,assgin标签
2,if标签
3,为空,或者默认值的处理
4,escape标签
5,内置函数和格式的支持
6,macro模板功能
7,集合的遍历
8*,switch标签
9*,FreeMarker中<#include>和<#import>标签的区别
10*,截取指定文本长度

建议:
1,对一些符号,字符串中所有的特殊 HTML 字符都需要用实体引用来代替
2,使用escape标签,过滤特殊符号

主要内容:
1,assgin
是自定义变量,可以对变量进行自定义处理,如:

<#assign name=value>
or
<#assign name1=value1 name2=value2 ... nameN=valueN>
or
<#assign same as above... in namespacehash>
or
<#assign name>
  capture this
</#assign>
or
<#assign name in namespacehash>
  capture this
</#assign>

2,if标签
if是判断选择的标签,如:

<#if condition>
  ...
<#elseif condition2>
  ...
<#elseif condition3>
  ...
...
<#else>
  ...
</#if>

3,为空,或者默认值的处理
不知道一个变量是否为空或者null,则可以以下方式使用

${user!}

若有默认值,则可以为:

${user.name!'匿名'}

若检测一个对象可以如下处理:unsafe_expr??或(unsafe_expr)??
若对象不存在,则直接隐藏掉

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

4,escape标签
escape标签会自动对一些符号进行转译,减少人工干预和可能的注入攻击,noescape标签里则可以加入html标签符号:

<#escape x as x?html> 
 ... 
 <p>Title: ${book.title}</p> 
 <p>Description: 
<#noescape>${book.description}</#noescape></p> 
 <h2>Comments:</h2> 
 <#list comments as comment> 
 <div class="comment"> 
 ${comment} 
 </div> 
 </#list> 
 ... 
</#escape>

5,内置函数和格式的支持
cap_first:字符串的第一个字母变为大写形式
lower_case:字符串的小写形式
upper_case:字符串的大写形式
trim:去掉字符串首尾的空格
size:序列中元素的个数
int:数字的整数部分(比如-1.9?int 就是-1)
这里只列一些还有许多需要自己再需要的时候去查找

${test?html} 
${test?upper_case?html} 
${seasons?size} 
${seasons[1]?cap_first} <#-- left side can by any expression --> 
${"horse"?cap_first} 

输出结果:
Tom & Jerry
TOM & JERRY
4
Spring
Horse

其次就是格式的设置:
通过String(“xxx:xx:xx”)可对格式进行设置

/*date,time,datetime三种都可以指定格式*/
<#assign lastUpdated = "2009-01-07 15:05"?datetime("yyyy-MM-dd HH:mm") />
      ${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")};
      ${lastUpdated?string("EEE,MMM d,yy")};
      ${lastUpdated?string("EEEE,MMMM dd,yyyy,hh:mm:ss a '('zzz')'")};

对一些字符转为时间格式也可以这样设置:

<#setting datetime_format="yyyy-MM-dd HH:mm"/>
/*如果字段是字符串格式,就要先转换为number,支持链式调用*/
${profile.createTime?number?number_to_datetime}

6,macro模板功能
首先使用include标签,把macro的定义包含进去:
<#include “/macrotemplate.html”>文件内容如下:

/*greet是模板名,person是参数名  */
<#macro greet person> 
 <font size="+2">Hello ${person}!</font> 
</#macro> 
/*nested 是内嵌内容*/
<#macro border> 
 <table border=4 cellspacing=0 cellpadding=4><tr><td> 
 <#nested> 
 </td></tr></table> 
</#macro> 

如果调用:<@border>The bordered text</@border>
则会输出结果:

<table border=4 cellspacing=0 cellpadding=4><tr><td> 
 The bordered text 
 </td></tr></table> 

如果调用<@greet person="Joe"> Anything. </@greet>
就会得到结果:

<font size="+2">Hello Joe!</font> 

复杂例子:

<#macro repeat count> 
 <#list 1..count as x> 
 <#nested x, x/2, x==count> 
 </#list> 
</#macro> 
<@repeat count=4 as c, halfc, last> 
 ${c}. ${halfc}
 <#if last> Last!</#if> 
</@repeat>

得到结果:
1. 0.5
2. 1
3. 1.5
4. 2 Last!

使用完整例子:
模板:

<#macro copyright date> 
 <p>Copyright (C) ${date} Julia Smith. All rights reserved. 
 <br>Email: ${mail}</p> 
</#macro> 

调用:

<#import "/lib/my_test.ftl" as my> 
<#assign mail="fred@acme.com"> 
<@my.copyright date="1999-2002"/> 
${my.mail} 
${mail} 

结果:

<p>Copyright (C) 1999-2002 Julia Smith. All rights reserved. 
 <br>Email: jsmith@acme.com</p> 
jsmith@acme.com 
fred@acme.com 

7,集合的遍历
freemarker关于集合遍历主要也就是list集合,map集合,可能还有数组
1)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>

2)map集合遍历
对于map里的是user对象则可以

<#if resourcemap?exists>
<#list resourcemap?keys as key> 
  <h6>
        ${key}<br/>
       <#assign item = resourcemap[key]>
       ${item.name!}
  </h6>
</#list>
</#if>

3)数组的遍历 跟list遍历类似 不再复述

8,switch标签
多值的判断选择:

<#switch animal.size>
  <#case "small">
     This will be processed if it is small
     <#break>
  <#case "medium">
     This will be processed if it is medium
     <#break>
  <#case "large">
     This will be processed if it is large
     <#break>
  <#default>
     This will be processed if it is neither
</#switch>  

9,FreeMarker中<#include>和<#import>标签的区别
<#include>:该标签的作用是将便签中指定的路径的ftl文件导入到使用标签的ftl文件中,包括macro\funtion\variable等所有被引用的ftl内容。
<#import>其作用是将service.ftl中的定义的各宏、函数、变量、自定义、设置等内容用指定的命名空间名称加以引用。但是当前文档不会将import的模板输出插入到import标签的位置。和<#include>标签一样可以使用相对路径和绝对路径引用外部模板。

10,截取指定的文本长度

 ${content?substring(0,100)}... //或者
 ${content[0..100]}... 

建议:
1,字符串中所有的特殊 HTML 字符都需要用实体引用来代替
如:用<代替<,<e;代替<=,>代替>,>e; 代替>=等等

参考文献:
1,南磊 翻译的freemarker中文手册 2.3.19
http://sourceforge.net/projects/freemarker/files/chinese-manual/FreeMarker_Manual_zh_CN.pdf/download
2,freemarker官方文档:http://freemarker.org/docs/ref_directive_if.html
3,freemarker中include和import的区别:http://m.blog.csdn.net/blog/xieguojun2008/17529987

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值