首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > Web前端 >

freemaker惯用

2012-11-23 
freemaker常用?freemarker学习笔记--常用命令2008-10-27 18:02freemarker学习笔记--常用命令?1.条件语句 i

freemaker常用

?

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

?

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标记类似,但用#开始(有些以@开始,在后面叙述)。?

?

注释:包含在<#-- 和 -->(而不是<!--和-->)之间文本。

?

?

?

?

?

1.action代码写法不变

配置方法

修改类型:freemarker

<action name="TestAction" name="success">/test/test.html</result>

</action>

?

热点排行