这里给已经了解 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}
- 检索变量
- 字符串操作
- 插值(或连接):
"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)??
- 赋值操作:
=, +=, -=, *=, /=, %=, ++, --