用了一年多的freemarker,用得多了,碰到的语法就越来越多,觉得有必要做一个总结,总结我所见到且有用的,这个语法总结以后碰到新的freemarker语法也会更新进来,也算是做笔记了,好记性不如烂笔头嘛~
一、输出变量1、字符串
${data.name?if_exists} // 变量存在,输出该变量,否则不输出${data.name!} // 变量存在,输出该变量,否则不输出2、日期
${data.datetime?string("yyyy年MM月dd日")}${data.datetime?string('yyyy-MM-dd HH:mm:ss')}${data.datetime?string('yyyy年MM月dd日 HH时mm分ss秒')}3、默认值
${data.name?default('XX')} // 变量不存在,取默认值XX${data.name!"xxx"} // 变量不存在,取默认值xxx4、数字格式
${data.money?string.number} // 输出80${data.money?string.currency} // ¥80.00 ${data.money?string.percent} // 80%${2.333?int} // 将小数转为int,输出2${'111.11'?number} // 输出111.115、布尔类型
<#assign bl=true />${bl?string("yes","no")} //输出yes${bl?string} //输出true注意:布尔值必须转换为字符串才能输出 二、内部函数的使用1、使用+号来拼接字符串
${"您好,"+name}${"您好,${name}"}${str + name}2、使字符串第一个字母大写-?cap_first
${"freemarker"?cap_first} //输出Freemarker3、将字符串转换成大写-?upper_case
${"Str"?upper_case} //输出STR4、将字符串转换成小写-?lower_case
${"Str"?lower_case} //输出str5、定义一个字符串-assign
<#assign str='freemarker'/>6、去掉字符串前后的空白字符-?trim
<#assign str='freemarker '/>${str!}---${str?if_exists?trim}--- //输出freemarker ---freemarker---7、获取字符串的长度-?length
${"freemarker"?length} //输出108、截取字符串输出-?substring
<#assign str = "freemarker"/>${str?substring(0,4)} //输出free${str?substring(1,4)} //输出ree${str?substring(4)} //输出marker9、取第一次出现的索引-?index_of
<#assign str = "freemarker"/>${str?index_of('e')} //输出2${str?index_of('f')} //输出0${str?index_of('m')} //输出410、取最后一次出现的索引-?last_index_of
<#assign str = "freemarker"/>${str?last_index_of('e')} //输出8${str?last_index_of('f')} //输出0${str?last_index_of('m')} //输出411、字符串替换-?replace
<#assign str = "abcdc"/>${str?replace('c','##')} //输出ab##d##${str?replace('d','@@')} //输出abc@@c12、字符串分割-?split
<#assign str = "a_b_c"/><#list str?split("_") as item>${item}</#list> //输出abc13、判断字符串是否包含指定的子字符串-?contains
<#assign str = "freemarker"/>${str?contains('rr')?string} //输出false${str?contains('ee')?string} //输出true${str?contains('m')?string} //输出true注意:布尔值必须转换为字符串才能输出,否则会出现freemarker.core.NonNumericalException错误注意:布尔值必须转换为字符串才能输出,否则会出现freemarker.core.NonNumericalException错误
14、判断是否以给定的子字符串开头-?starts_with
<#assign str = "freemarker"/>${str?starts_with('ker')?string} //输出false${str?starts_with('free')?string} //输出true注意:布尔值必须转换为字符串才能输出,否则会出现freemarker.core.NonNumericalException错误
15、判断是否以给定的子字符串结尾
<#assign str = "freemarker"/>${str?starts_with('ker')?string} //输出true${str?starts_with('free')?string} //输出false注意:布尔值必须转换为字符串才能输出,否则会出现freemarker.core.NonNumericalException错误
三、逻辑判断组合写法
1、if判断
<#if condition1>...<#elseif condition2>...<#elseif condition3>...<#else>...</#if>2、switch选择判断
<#switch value><#case refValue1>...<#break><#case refValue2>...<#break>...<#case refValueN>...<#break><#default>...</#switch>3、定位替换
<#if item?? > <#if item.username?? && item.username!=""> ${item.username?replace((item.username?substring(item.username?index_of("-")+1,item.username?index_of("-")+2)),"*")} </#if></#if>4、list列表输出
<table class="form-table" cellpadding="0" cellspacing="1px"><tr> <th nowrap="nowrap">姓名</th><th nowrap="nowrap">性别</th></tr><!-- 此处,加入查询结果对象的判断,然后插入数据到数据表 --><#if dataList??><#if (dataList?size > 0)> <#list dataList as data><tr><td nowrap="nowrap">${data.username?if_exists}</td><td nowrap="nowrap">${data.sex?if_exists}</td></tr></#list><#else> <tr> <td colspan="19"> 未查询到相关数据,请更改查询条件后重新查询! </td> </tr> </#if> </#if></table>5、循环list列表输出,可用于脱敏显示
//data.name?if_exists?length等于3<#list 0..(data.name?if_exists?length)!0 as i>*</#list> //0至3,输出四次,输出****<#list 2..(data.name?if_exists?length)!0 as i>*</#list> //2至3,输出四次,输出**