(3)表達(dá)式
l 直接指定值
Ø 字符串
n 使用單引號(hào)或雙引號(hào)限定
n 如果包含特殊字符需要轉(zhuǎn)義,如下面的例子:
${"It's \"quoted\" and
this is a backslash: \\"}
${'It\'s "quoted" and
this is a backslash: \\'}
輸出結(jié)果是:
It's "quoted" and
this is a backslash: \
It's "quoted" and
this is a backslash: \
n 下面是支持的轉(zhuǎn)義序列:
轉(zhuǎn)義序列
|
含義
|
\"
|
雙引號(hào)(u0022)
|
\'
|
單引號(hào)(u0027)
|
\\
|
反斜杠(u005C)
|
\n
|
換行(u000A)
|
\r
|
Return (u000D)
|
\t
|
Tab (u0009)
|
\b
|
Backspace (u0008)
|
\f
|
Form feed (u000C)
|
\l
|
<
|
\g
|
>
|
\a
|
&
|
\{
|
{
|
\xCode
|
4位16進(jìn)制Unicode代碼
|
n 有一類特殊的字符串稱為raw字符串,被認(rèn)為是純文本,其中的\和{等不具有特殊含義,該類字符串在引號(hào)前面加r,下面是一個(gè)例子:
${r"${foo}"}
${r"C:\foo\bar"}
輸出的結(jié)果是:
${foo}
C:\foo\bar
Ø 數(shù)字
n 直接輸入,不需要引號(hào)
n 精度數(shù)字使用“.”分隔,不能使用分組符號(hào)
n 目前版本不支持科學(xué)計(jì)數(shù)法,所以“1E3”是錯(cuò)誤的
n 不能省略小數(shù)點(diǎn)前面的0,所以“.5”是錯(cuò)誤的
n 數(shù)字8、+8、08和8.00都是相同的
Ø 布爾值
n true和false,不使用引號(hào)
Ø 序列
n 由逗號(hào)分隔的子變量列表,由方括號(hào)限定,下面是一個(gè)例子:
<#list ["winter", "spring", "summer", "autumn"] as x>
${x}
</#list>
輸出的結(jié)果是:
winter
spring
summer
autumn
n 列表的項(xiàng)目是表達(dá)式,所以可以有下面的例子:
[2 + 2, [1, 2, 3, 4], "whatnot"]
n 可以使用數(shù)字范圍定義數(shù)字序列,例如2..5等同于[2, 3, 4, 5],但是更有效率,注意數(shù)字范圍沒(méi)有方括號(hào)
n 可以定義反遞增的數(shù)字范圍,如5..2
Ø 散列(hash)
n 由逗號(hào)分隔的鍵/值列表,由大括號(hào)限定,鍵和值之間用冒號(hào)分隔,下面是一個(gè)例子:
{"name":"green mouse", "price":150}
n 鍵和值都是表達(dá)式,但是鍵必須是字符串
l 獲取變量
Ø 頂層變量: ${variable},變量名只能是字母、數(shù)字、下劃線、$、@和#的組合,且不能以數(shù)字開(kāi)頭
Ø 從散列中獲取數(shù)據(jù)
n 可以使用點(diǎn)語(yǔ)法或方括號(hào)語(yǔ)法,假設(shè)有下面的數(shù)據(jù)模型:
(root)
|
+- book
| |
| +- title = "Breeding green mouses"
| |
| +- author
| |
| +- name = "Julia Smith"
| |
| +- info = "Biologist, 1923-1985, Canada"
|
+- test = "title"
下面都是等價(jià)的:
book.author.name
book["author"].name
book.author.["name"]
book["author"]["name"]
n 使用點(diǎn)語(yǔ)法,變量名字有頂層變量一樣的限制,但方括號(hào)語(yǔ)法沒(méi)有該限制,因?yàn)槊质侨我獗磉_(dá)式的結(jié)果
Ø 從序列獲得數(shù)據(jù):和散列的方括號(hào)語(yǔ)法語(yǔ)法一樣,只是方括號(hào)中的表達(dá)式值必須是數(shù)字;注意:第一個(gè)項(xiàng)目的索引是0
Ø 序列片斷:使用[startIndex..endIndex]語(yǔ)法,從序列中獲得序列片斷(也是序列);startIndex和endIndex是結(jié)果為數(shù)字的表達(dá)式
Ø 特殊變量:FreeMarker內(nèi)定義變量,使用.variablename語(yǔ)法訪問(wèn)
${"Hello ${user}!"}
${"${user}${user}${user}${user}"}
${"Hello " + user + "!"}
${user + user + user + user}
<#if ${isBig}>Wow!</#if>
<#if "${isBig}">Wow!</#if>
<#if isBig>Wow!</#if>
${user[0]}${user[4]}
${user[1..4]}
BJ
ig J
<#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>
- ${user}
</#list>
- Joe
- Fred
- Julia
- Kate
<#assign ages = {"Joe":23, "Fred":25} + {"Joe":30, "Julia":18}>
- Joe is ${ages.Joe}
- Fred is ${ages.Fred}
- Julia is ${ages.Julia}
- Joe is 30
- Fred is 25
- Julia is 18
${x * x - 100}
${x / 2}
${12 % 10}
-75
2.5
2
${3 * "5"} <#-- WRONG! -->
${3 + "5"}
35
${(x/2)?int}
${1.1?int}
${1.999?int}
${-1.1?int}
${-1.999?int}
2
1
1
-1
-1
<#if x < 12 && color = "green">
We have less than 12 things, and they are green.
</#if>
<#if !hot> <#-- here hot must be a boolean -->
It's not hot.
</#if>
${test?html}
${test?upper_case?html}
Tom & Jerry
TOM & JERRY
操作符組
|
操作符
|
后綴
|
[subvarName] [subStringRange] . (methodParams)
|
一元
|
+expr、-expr、!
|
內(nèi)建
|
?
|
乘法
|
*、 / 、%
|
加法
|
+、-
|
關(guān)系
|
<、>、<=、>=(lt、lte、gt、gte)
|
相等
|
==(=)、!=
|
邏輯and
|
&&
|
邏輯or
|
||
|
數(shù)字范圍
|
..
|
<#setting number_format="currency"/>
<#assign answer=42/>
${answer}
${answer?string} <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}
$42.00
$42.00
42
$42.00
4,200%
${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
${lastUpdated?string("EEE, MMM d, ''yy")}
${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}
2003-04-08 21:24:44 Pacific Daylight Time
Tue, Apr 8, '03
Tuesday, April 08, 2003, 09:24:44 PM (PDT)
<#assign foo=true/>
${foo?string("yes", "no")}
yes
<#-- If the language is US English the output is: -->
<#assign x=2.582/>
<#assign y=4/>
#{x; M2} <#-- 2.58 -->
#{y; M2} <#-- 4 -->
#{x; m1} <#-- 2.6 -->
#{y; m1} <#-- 4.0 -->
#{x; m1M2} <#-- 2.58 -->
#{y; m1M2} <#-- 4.0 -->
|