分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

在HTML代码中使用freemarker

发布时间:2023-09-06 02:16责任编辑:赖小花关键词:HTML

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

在HTML代码中使用freemarker

原文地址:https://www.cnblogs.com/gjq1126-web/p/9736136.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved