Freemarker指令
来源:互联网 发布:百马百担c语言编程 编辑:程序博客网 时间:2024/05/16 08:45
一:if指令:
<#if condition>...
<#elseif condition2>...
<#elseif condition3>...
...<#else>...
</#if>
二:switch
<#switch value>
<#case refValue1>...<#break>
<#case refValue2>...<#break>...
<#case refValueN>...<#break>
<#default>...</#switch>
三:list,break
<#list sequence as item>
...
</#list>
两个特殊的循环变量:
item_index: 当前变量的索引值。
item_has_next: 是否存在下一个对象。
可以用<#break/>指令离开loop循环。
四:include指令
<#include filename>或者
<#include filenameoptions>
filename: 表达式转换为字符串
options: encoding=encoding, parse=parse encoding: 表达式转换为字符串
parse: 表达式转换为逻辑值,是否作为ftl文件解析。
<#include "/common/navbar.html" parse=false encoding="Shift_JIS">
<#include "*/footer.ftl">表示当前目录下以及所有父目录下的文件。如果当前模版在/foo/bar/template.ftl ,那么查找footer.ftl的顺序为:/foo/bar/footer.ftl
/foo/footer.ftl/footer.ftl这种方式讲允许设计者将通用的模版放在父文件夹里面。
也可以只指定部分路径:<#include "*/commons/footer.ftl">
五:import指令<#import path as hash>
六:noparse
<#noparse>
...
</#noparse>
七:compress
<#compress>
...
</#compress>
八:escape,noescape
<#escape identifier as expression>
...
<#noescape>...</#noescape>
...
</#escape>
escape指令body区的ftl的interpolations都会被自动加上escape表达式。但是不会影响字符串内的interpolations。而且也只会影响到body内出现的interpolations,比如不会影响到include的ftl的interpolations。
<#escape x as x?html>
First name: ${firstName}
Last name: ${lastName}
Maiden name: ${maidenName}</#escape>
等同于:
Last name: ${lastName?html}
Maiden name: ${maidenName?html}
escape指令在解析模版时起作用,而不是运行时起作用。
escape指令还能嵌套使用,子继承父的escape规则。
<#escape x as x?html>
Customer Name: ${customerName}
Items to ship:
<#escape x as itemCodeToNameMap[x]>
${itemCode1}
${itemCode2}
${itemCode3}
${itemCode4}
</#escape></#escape>
等同于:Customer Name: ${customerName?html}
Items to ship:
${itemCodeToNameMap[itemCode1]?html}
${itemCodeToNameMap[itemCode2]?html}
${itemCodeToNameMap[itemCode3]?html}
${itemCodeToNameMap[itemCode4]?html}
九:assign指令
<#assign name=value>or<#assign name1=value1name2=value2... nameN=valueN>or<#assign same as above... in namespacehash>or<#assign name> capture this</#assign>or<#assign name in namespacehash> capture this</#assign>创建或者替换一个已经存在的变量,只能作用于顶层变量。不好的做法:<#assign x>Hello ${user}!</#assign>更改为:<#assign x="Hello ${user}!">十:global指令
<#global name=value>
or
<#global name1=value1name2=value2... nameN=valueN>or<#global name>
capture this
</#global>
十一:local指令
<#local name=value>
or
<#local name1=value1name2=value2... nameN=valueN>or<#local name>
capture this
</#local>只能用于macro的定义body中。
十二:setting指令:
<#setting name=value>
name的取值范围:
local:number_format:
boolean_format:缺省值为"true,false"
date_format, time_format, datetime_format
time_zone:url_escaping_charset
classic_compatible
十三:用户自定义指令
<@macro_or_transfparam1=val1param2=val2...paramN=valN/><@macro_or_transfparam1=val1param2=val2...paramN=valN ; lv1, lv2, ..., lvN/><@macro_or_transf...> ...</@macro_or_transf><@macro_or_transf...> ...</@><@macro_or_transfval1, val2, ..., valN/>十四:macro,nested,return
<#macro nameparam1param2... paramN>
...
<#nested loopvar1, loopvar2, ..., loopvarN>
...
<#return>
...
</#macro>
十五:function, return
<#function nameparam1param2... paramN>
... <#return returnValue>
...
</#function>
<#function avg x y>
<#return (x + y) / 2>
</#function>
${avg(10, 20)}
十六:flush
<#flush>
十七:stop
<#stop>
or
<#stop reason>取消处理模版。
十八:ftl指令
<#ftl param1=value1param2=value2...paramN=valueN>
ftl指令必须放在ftl文件的最上面。
参数范围:
encoding:
strip_whitespace
strip_text
strict_syntax
十九:t, lt, rt
二十:attempt, recover
<#attempt>
attempt block<#recover>
recover block
</#attempt>
Optional content: ${thisMayFails}
<#recover>
Ops! The optional content is not available.
</#attempt>
[转载地址:http://fred2005.javaeye.com/blog/289336]
- freemarker指令
- Freemarker指令
- Freemarker指令
- FreeMarker 指令
- FreeMarker指令
- FreeMarker 指令
- [Freemarker] freemarker自定义指令
- [转]freemarker 基本指令
- FreeMarker-if指令
- freemarker的指令
- freemarker的自定义指令
- FreeMarker自定义指令
- FreeMarker 基本指令
- freemarker入门 list指令
- 入门freemarker include指令
- freemarker 常用指令
- Freemarker之List指令
- freemarker 基本指令
- web developer tips (58):在vs里导入导出每一个HTML的标记
- 11.03[转自:Jack @ CnBlogs]实现iFrame自适应高度,原来很简单!
- 当printf遇到了i++ 或++i
- Android 实验室
- 关于“#ifdef __cplusplus”
- Freemarker指令
- 基于SQL Server 分页存储过程的演进
- 关于WINCE内版本的说明
- 液晶电视与等离子电视的区别
- MyEclipse 的快捷键
- php基础问题(无答案)
- javascript typeof的用法
- pcanywhere 出现加载驱动程序错误的提示
- How many consecutive zeros at the ends of N! ?