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