freemarker学习笔记--常用命令

freemarker学习笔记--常用命令
2008-10-27 18:02

freemarker学习笔记--常用命令

1.条件语句 if
<#if !readonly && !replyOnly>
your code
</#if>

注:if后面跟的是判断条件,判断条件不能用${变量名}的形式,直接用变量名就可以了

2.条件判断语句 if else
<#if (session.privateMessages > 0)>
${I18n.getMessage("ForumBase.newPm")}: (${session.privateMessages})
<#else>
${I18n.getMessage("ForumBase.privateMessages")}
</#if>

注:if else语句跟普通的不同之处是在中间可以有多句代码,而不是只执行if后面的一句代码

3.嵌套条件判断语句if if else
<#if logged>
<#if !readonly && !replyOnly>
   your code
<#/if>

other code

<#if (session.privateMessages > 0)>
   ${I18n.getMessage("ForumBase.newPm")}: (${session.privateMessages})
<#else>
   ${I18n.getMessage("ForumBase.privateMessages")}
</#if>

other code
</#if>

 

4.循环指令 list
<#list allCategories as category>
you code
</#list>

<!-- 1~3的循环 -->
<#list 1 .. 3 as page>
<@pageLink page, id/>
</#list>

4.嵌套循环指令 list
<#list allCategories as category>
your code:${category.name}

<#list category.getForums() as forum>
   inner code:${category.forum.name}
   <#list moderators as m>
    ${m_index + 1}.${m.name?html}
   </#list>
</#list>

<#if category.name = "">
<#break>
</#if>

other code
</#list>

注:as后面是别名,在循环体中可直接用(类似sql语句表的别名,以用<#break>指令提前结束list循环

。${m_index + 1}.是取list的索引,其中m_index为list的下标,从0开始。

5.导入macros指令 import
<#import "../macros/pagination.ftl" as pagination>
使用:<@pagination.doPagination action, forum.id/>
//<@pagination 为引用macros模板 pagination为对应模板文件的别名
//pagination.doPagination action 为模板文件里面的一个macro--<#macro doPagination action

id=-1>

6.定义变量指令 assign
<!-- 定义一个变量moderators 只能作用于同层或下层 -->
<#assign moderators = forumRepository.getModeratorList(forum.id)/>
<#list moderators as m>
${m.name?html}
</#list>
//其中forumRepository.getModeratorList(forum.id)为变量moderators的值

<#-- define a variable to come from pagination.ftl file name as doPagination action macro-

->
<#assign paginationData><@pagination.doPagination action, forum.id/></#assign>
${paginationData}
//其中<@pagination.doPagination action, forum.id/>为变量paginationData的值

7.改变(替换)变量指令 assign
<#assign total = forum.totalPosts/>
<!-- 定义一个变量total 值为forum.totalPosts -->
<#if (total == 0 && forum.totalTopics > 0)>
<!-- 替换原来变量的值,以后引用total变量的值为forum.totalTopics -->
<#assign total = forum.totalTopics/>
</#if>

8.switch指令
<#switch being.size>
<#case "small">
    This will be processed if it is small
    <#break>
<#case "medium">
    This will be processed if it is medium
    <#break>
<#case "large">
    This will be processed if it is large
    <#break>
<#default>
    This will be processed if it is neither
</#switch>

<#escape x as x?html>
userName: ${userName}
</#escape>
等同于
userName: ${userName?html}

注:escape指令在解析模版时起作用,而不是运行时起作用


9.设置缺省格式指令setting
<#setting number_format = "#"/>
${1.234}
输出1
<#setting number_format="0.##">
${1.234}
输出
1.23

10.包含文件指令 include
<#include "header.htm"/>


除了相关的文本外,在FreeMarker模板中可以包括下面三种特定部分:
${…}:称为插补(interpolations),FreeMarker会在输出时用实际值进行替代。
指令:也叫FreeMarker标记,与HTML标记类似,但用#开始(有些以@开始,在后面叙述)。
注释:包含在<#-- 和 -->(而不是<!--和-->)之间文本。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值