您的位置:首页 > 其它

freemarker笔记

2016-06-20 17:58 281 查看
${.....} ===> ${xxxx},${xxx.xxx.xxx}

if指令 ===> <#if user=="bin" >html </#if> 或 <#if user="bin" >html <#else>html </#if>
<#if price==0>html </#if> 或 <#if price==0>html <#else>html </#if>
<#if animals.python.price>animals.elephont.price>html </#if> 或 <#if animals.python.price>animals.elephont.price>html <#else>html </#if>
list指令===> <#list users as user>html</#list>或 <#list users as user><#if user.name="bin">html</#if></#list>
include指令 ===> <#include "xxx/xxx.html">

处理不存在的变量:<h1>Welcome ${user!"Anonymous"}!</h1> 或 <#if user??><h1>Welcome ${user}!</h1></#if> 或 <#if (animals.python.price)!0><h1>Welcome ${user}!</h1></#if>

标量:字符串,数字,布尔值,日期
容器:哈希表(user.name,user.age),序列(number[1],number[2]),集
字符串操作: str="abcdefg";===> "Hello ${user.name}" "Hello"+" bin"; str[0]等于a
序列操作:users=["bin","job","co","jame"] ===> users+["haha"] ,users[1..3] ,users[3..]
哈希表操作:user={"age":20,"name":"bin"} ===> user+{"addr":"address"} 拼接过程中,key一样的覆盖,其它追加;取值法:book.author.name, book["author"].name, book.author.["name"],book["author"]["name"]

处理不存在的值:默认值:name!"unknown" 或者(user.name)!"unknown" 或者 name! 或者 (user.name)! <#if mouse??> 使用内建函数处理不存在的值: default,exists 和if_exists

***注意点: ${3*"5"}不执行,${3+"5"}可执行,值为“35”

比较运算: = ,!=,<=;逻辑运算: || ,&&,!

****注意点:字符串中有特殊的html字符都需要实体引用代替,<代替<

内建函数:cap_first:字符串的第一个字母变为大写形式 lower_case:字符串的小写形式 upper_case:字符串的大写形式 trim:去掉字符串首尾的空格 size:序列中元素的个数 int:数字的整数部分
函数调用:${test?upper_case?html} test字符串,调用upper_case 转成大写字母,之后再调用html将test中属于html的特殊字符转成实体引用
方法的调用:${repeat("What", 3)}将“What”字符串拷贝三次

实例:
${mouse!"No mouse."} ===>变量不存在,显示No mouse
<#assign mouse="Jerry"> ===>为变量赋值
${mouse!"No mouse."} ===>变量存在,显示变量值Jerry


插值: ${x+100} ${name}! <#include "xxxx/${url}.html">

日期转成字符串: time_format,date_format 和datetime_format


自定义指令:

宏指令 定义1:
<#macro greet>
<font size="+2">Hello Joe!</font>
</#macro>
调用:<@greet></@greet> 或<@greet/>
定义2:
<#macro greet person color ...>
<font size="+2">Hello Joe!</font>
</#macro>
调用:<@greet person="Fred" color="black" .../>

实例

<#macro repeat count> ===>定义一个宏
<#list 1..count as x> ===> 遍历一个 1..count 1到count的序列
<#nested x, x/2, x==count> ===>将 x=1,x=2,x=3...依次传到返回值里
</#list>
</#macro>

<@repeat count=4 ; c, halfc, last>
${c}. ${halfc}<#if last> Last!</#if> ===> 输出值
</@repeat>

创建变量:<#assign x = 1>;变量范围:内部变量隐藏外部变量,获取全局变量:${.globals.user}

引入外部模板(import 指令):<#import "/lib/my_test.ftl" as my> ===>调用:<@my.copyright date="1999-2002"/> ${my.mail}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: