==============常用语法=================
三.
EG.一个对象BOOK
1.输出 ${book.name}
空值判断:${book.name?if_exists },
${book.name?default(‘xxx’)}//默认值xxx
${ book.name!"xxx"}//默认值xxx
日期格式:${book.date?string('yyyy-MM-dd')}
数字格式:${book?string.number}--20
${book?string.currency}--<#-- $20.00 -->
${book?string.percent}—<#-- 20% -->
插入布尔值:
<#assign foo=ture />
${foo?string("yes","no")} <#-- yes -->
=============逻辑判断==================
2.
a:
<#if condition>...
<#elseif condit
inc 发布于2007-09-08 16:18:57
ion2>...
<#elseif condition3>......
<#else>...
其中空值判断可以写成<#if book.name?? >
b:
<#switch value>
<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
...
<#case refValueN>
...
<#break>
<#default>
...
===============循环读取=================
3.
<#list sequence as item>
...
空值判断<#if bookList?size = 0>
e.g.
<#list employees as e>
${e_index}. ${e.name}
<#if mole?default(0)!=0>
=============用来压缩空白空间和空白的行===============
用例
<#assign x = " moo ">
(<#compress>
1 2 3 4 5
${moo}
test only
I said, test only
)
输出
(1 2 3 4 5
moo
test only
I said, test only)
==============Sequence内置的计数器==============
3. Sequence内置的计数器: xxx_index
用途:显示序号
模板:
<#list employees as e>
${e_index}. ${e.name}
输出:
1. Readonly
2. Robbin
==============Sequence内置的分段器=====================
4. Sequence内置的分段器: chunk
用途:某些比较BT的排版需求
模板:
<#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']>
<#list seq?chunk(4) as row>
<#list seq?chunk(4, '-') as row>
<#list row as cell>${cell}
输出:
a
b
c
d
e
f
g
h
i
j
-
-
=====================freemarker的list================================
Scalar String:${scalarString}
Scalar Number:${scalarNumber}
Object is:${scalarObject}
List使用样例-List元素为Scalar对象:
<#list scalarList as value0>
Scalar List值:${value0}
List使用样例-List元素为User对象:
<#list userList as listUser>
List对象User Id值:${listUser.userId}
Map使用样例-Map Values元素为Scalar :
<#list scalarMap?keys as mykey>
Scalar Map key is :${mykey}
Scalar Map value is:${scalarMap[mykey]}
Map使用样例-Map Values元素为User对象:
<#list userMap?keys as key1>
<#assign mapUser="${userMap[key1]}" >
User Object is :${mapUser}
<#--
以下方法有问题
User is :${mapUser.userId}
-->
==============FreeMarker中list排序==============
升序:
<#list list?sort_by("time") as v>
降序:
<#list list?sort_by("time") as v>
===============freemarker在模板中定义变量==============
在模板中定义的变量有三种类型:
plain变量:可以在模板的任何地方访问,包括include指令插入的模板,使用assign指令创建和替换
<#include "/WEB-INF/index/top.html">
==============freemarker.properties配置================
(1)解决输出中文乱码问题:
default_encoding=UTF-8
locale=zh_CN
number_format=#
(2)提高freemarker的性能
template_update_delay=60000
(3)freemarker的标签种类:
${..}
# 代表是FTL tags
<#if ...>
<#list totalList as elementObject>...
@ ,代表用户自定义的标签
<#-- --> 注释标签,注意不是
=====================将图片整除换行====== <#if (u_index+1)%4=0>当图片超过五个就换行=======
|
===============================freemarker中在application
${Application.web_title}
在类里:
list=dser.indexvideo(cvo);
if(list.size()!=0){
map.put("最新",list);}
在html静态页里:
<#import "/WEB-INF/ftl/index.ftl" as jf>
(index.ftl
<#macro greet title len>
<#if title?length != 0>
<#if (title?length>len)>
${title[0..len-1]} <#else>
${title?trim}
<#else>
${title?default("")}
<#--时间比较-->
<#macro vstime start end>
<#if start?default("")==""||end?default("")=="">
--
<#else>
<#assign starts=start?replace("-","") >
<#assign ends=end?replace("-","") >
<#if (starts?number>ends?number)>
以过期
<#else>
未过期
)
<#if map["最新"]?exists>
<#list map["最新"] as u>
${u.filepath?default('')}
<@jf.greet title=u.title len=6 />