FreeMarker - 内建函数

【1】Sequence的内置函数

1.sequence?first 返回sequence的第一个值。

  array: ${cityArray?first}
  list: ${cityList?first}
  set: ${citySet?first}

2.sequence?last 返回sequence的最后一个值。

3.sequence?size 返回sequence的大小。

4.sequence?reverse 将sequence的现有顺序反转,即倒序排序

5.sequence?sort 将sequence中的对象转化为字符串后顺序排序

正    序:<#list cityArray as city>${city},</#list>
倒    序:<#list cityArray?reverse as city>${city},</#list>
升    序:<#list cityArray?sort as city>${city},</#list>
降    序:<#list cityArray?sort?reverse as city>${city},</#list>

6.sequence?sort_by(value) 按sequence中对象的属性value进行排序

7.获取sequence当前元素的索引

<#list sequence as item>
    ${item_index}//获得当前元素索引值
</#list>

8.根据索引获取sequence中的指定元素

array: ${cityArray[0]}
list: ${cityList[0]}
set:  ${citySet[0]}

注意:Sequence不能为null


【2】Hash的内置函数

1.hash?keys 返回hash里的所有key,返回结果为sequence

<#assign mapKeys=cityMap?keys/>
<#list mapKeys as mapKey>
    ${mapKey}
</#list>

2.hash?values 返回hash里的所有value,返回结果为sequence

<#assign mapValues=cityMap?values/>
<#list mapValues as mapValue>
    ${mapValue}
</#list>

3.遍历map元素

map 通过key获取value的方法用[]
<#list cityMap?keys as  key>
   ${key_index} --> ${key} --> ${cityMap[key]}
</#list>

【3】操作字符串内置函数

1.表达式?substring(start,end)从一个字符串中截取子串

start:截取子串开始的索引,start必须大于等于0,小于等于end;

end: 截取子串的长度,end必须大于等于0,小于等于字符串长度,如果省略该参数,默认为字符串长度。

${'EFGHIJKL'?substring(0)}  
${'EFGHIJKL'?substring(0,1)}  

2.cap_first 将字符串中的第一个单词的首字母变为大写。

3.uncap_first将字符串中的第一个单词的首字母变为小写。

4.capitalize将字符串中的所有单词的首字母变为大写。

5.lower_case将字符串转为小写。

6.upper_case将字符串转为大写。

 1. 首个单词的首字母大写: ${"hello world"?cap_first}
 2. 首个单词的首字母小写: ${"Hello World"?uncap_first}
 3. 所有单词首字母大写:${"hello world"?capitalize}
 4. 字符串大写: ${"hello,world"?upper_case}
 5. 字符串小写:${"hello,world"?lower_case}

7.date,time,datetime将字符串转换为日期

${dateValue?date}
${dateValue?time}
${dateValue?datetime}
格式化日期: ${dateValue?string('yyyy-MM-dd HH:mm:ss')}

注意:如果指定的字符串格式不正确将引发错误


7.5 starts_with判断是否以xxx 开头

${"hello,world"?starts_with("hello")?string}    

<#if "hello,world"?starts_with("hello")>
  hello,world 以字符串 hello 开头
</#if>

8.ends_with 判断某个字符串是否由某个子串结尾,返回布尔值

${"hello,world"?ends_with("world")?string}  

<#if "hello,world"?ends_with("world")>
 hello,world 以字符串 world 结尾
</#if>

注意:布尔值必须转换为字符串才能输出


9.html 用于将字符串中的<、>、&和”替换为对应得<>&quot:&amp

8.index_of(substring,start)在字符串中查找某个子串,返回找到子串的第一个字符的索引,如果没有找到子串,则返回-1。

Start参数用于指定从字符串的那个索引处开始搜索,start为数字值。
如果start大于字符串长度,则start取值等于字符串长度,如果start小于0,则start取值为0。

${"hello,world"?index_of("o")}
${"hello,world"?index_of("aaa")}

10.length返回字符串的长度

 ${"hello,world"?length}

12.contains 判断字符中是否包含某个子串。返回布尔值

 ${"hello,world"?contains("llo")?string};

注意:布尔值必须转换为字符串才能输出

13.number将字符串转换为数字

14.replace用于将字符串中的一部分从左到右替换为另外的字符串。

15.split使用指定的分隔符将一个字符串拆分为一组字符串

16.trim 删除字符串首尾空格

${"   hello,world   "?trim}

【4】操作数字内置函数

1.c 用于将数字转换为字符串

${data?c}  

2.string用于将数字转换为字符串

Freemarker中预订义了三种数字格式:number,currency(货币)和percent(百分比),其中number为默认的数字格式转换。

 货币:${0.3?string.currency}
 百分比:${0.3?string.percent}
 数字(默认):${0.3?string.number}

【5】操作布尔值内置函数

string用于将布尔值转换为字符串输出

true转为”true”,false转换为”false”

foo?string("yes","no")

如果布尔值(foo为布尔值)是true,那么返回”yes”,否则返回no

类似于三目运算符:

${(user.sex=="1")?string("男","女")}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

流烟默

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值