FreeMarker学习(常用表达式)
- 直接指定值
- 字符串:
"Foo"或者'Foo'或者"It's \"quoted\""或者'It\'s "quoted"'或者r"C:\raw\string" - 数字:
123.45 - 布尔值:
true,false - 序列:
["foo", "bar", 123.45]; 值域:0..9,0..<10(或0..!10),0.. - 哈希表:
{"name":"green mouse", "price":150}
- 字符串:
- 检索变量
- 顶层变量:
user - 从哈希表中检索数据:
user.name,user["name"] - 从序列中检索数据:
products[5] - 特殊变量:
.main
- 顶层变量:
- 字符串操作
- 插值(或连接):
"Hello ${user}!"(或"Hello " + user + "!") - 获取一个字符:
name[0] - 字符串切分: 包含结尾:
name[0..4],不包含结尾:name[0..<5],基于长度(宽容处理):name[0..*5],去除开头:name[5..]
- 插值(或连接):
- 序列操作
- 连接:
users + ["guest"] - 序列切分:包含结尾:
products[20..29], 不包含结尾:products[20..<30],基于长度(宽容处理):products[20..*10],去除开头:products[20..]
- 连接:
- 哈希表操作
- 连接:
passwords + { "joe": "secret42" }
- 连接:
- 算术运算:
(x * 1.5 + 10) / 2 - y % 100 - 比较运算:
x == y,x != y,x < y,x > y,x >= y,x <= y,x lt y,x lte y,x gt y,x gte y, 等等。。。。。。 - 逻辑操作:
!registered && (firstVisit || fromEurope) - 内建函数:
name?upper_case,path?ensure_starts_with('/') - 方法调用:
repeat("What", 3) - 处理不存在的值:
- 默认值:
name!"unknown"或者(user.name)!"unknown"或者name!或者(user.name)! - 检测不存在的值:
name??或者(user.name)??
- 默认值:
- 赋值操作:
=,+=,-=,*=,/=,%=,++,--
参考原文 http://freemarker.foofun.cn/dgui_quickstart_basics.html
SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
更多精彩

