在HTML代码中使用freemarker

在HTML代码中使用freemarker

1、freemarker中显示某对象的属性使用${user.name}.

但如果name为null,freemarker就会报错。如果需要判断对象是否为空:

<#if name??>.....</#if>

当然也可以通过设置默认值${name!''}来避免对象为空的错误。如果name为空,就以默认值(“!”后的字符)显示

对象user,name为user的属性的情况,user,name都有可能为空,那么可以写成${(user.name)!''},表示user或者name为null,都显示为空

判断为空:<#if (user.name)??>……</#if>

 

2、freemarker中截取字符串-------------------------------------

类型一:
<#if item.contents??>
  <#if item.contents?length gt 7>
    ${item.contents?substring(0,3)}****${item.contents?substring(7,item.contents?length)}
  <#else>${item.contents!}
  </#if>
</#if>
类型二:
<#if item.contents??>
  <#if item.contents?length gt 7>
    ${item.contents?substring(0,7)}****
  <#else>${item.contents!}
  </#if>
</#if>

<#if item.createTime??>
  <#if item.createTime?length gt 0>
    ${item.createTime?substring(0,10)}
  <#else>${item.createTime!''}
  </#if>
</#if>

<div title="${item.title!''}">${item.title?substring(0,9)}...</div>

 类型三:截取手机号的中间四位

 <#if phone??&&phone?length gt 7>${phone ? replace(phone ? substring(3,7),"****")}<#else>${phone!}</#if>

3、根据条件显示-------------------------------------
<td class="th18">
  <#if item.type?? && item.type =="0">天
  <#elseif item.type?? && item.type =="1">**
  <#elseif item.type?? && item.type =="2">**
  <#elseif item.type?? && item.type =="3">**
  <#elseif item.type?? && item.type =="4">**
  </#if>
</td>

 

4、freemark控制class样式-------------------------------------
<li class="biao-new <#if userConf??&&userConf.mothShortMark==1>active</#if>">

 

5、遍历  list  或者  page

<#if list  ?? && list  ?size gt 0>
  <#list channels as item>
    <option value="${item.id!}">${item.channelName!}</option>
  </#list>
</#if>

 

转载于:https://www.cnblogs.com/gjq1126-web/p/9736136.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值