## 13.2. 轉義 有效的 VTL 指令
某些情況使用Velocity可能會覺得很煩惱。逃避特殊符是處理出現在你的模板中VTL特殊符有效方法,就是使用反斜杠(“\”)。
```
#set( $email = "foo" )
$email
```
假如Velocity在你的模板中遇到$email,它會搜索上下文,得到相應的值。這里的輸出是foo,因為$email被定義了。假如$email沒有被定義,輸出會是$email。
設想$email被定義了(例如,它的值是foo),而且你想輸出$email。這里有幾種方法能達到目的,但是最簡單的是使用逃避符。
```
## The following line defines $email in this template:
#set( $email = "foo" )
$email
\$email
```
將顯示為:
```
foo
$email
```
注意到“\”屏蔽了左邊的“$”。屏蔽左邊規則,使得\\\$email顯示為\\$email。那些例子與$email沒有定義相比較。
```
$email
\$email
\\$email
\\\$email
```
將顯示為:
```
$email
\$email
\\$email
\\\$email
\\$email\\\$email
```
注意Velocity處理定義了的references與沒有定義的不一樣。這里set$foo的值為gibbous。
```
#set( $foo = "gibbous" )
$moon = $foo
```
輸出會是:`$moon=gibbous,$moon`按照字面上輸出因為它沒有定義,gibbous替代$foo輸出。避開VTL的directives還有其他方法,在Directives那章節會更詳細描述。
- 1. 關于
- 2. 什么是Velocity?
- 3. Velocity 可以做什么?
- 3.1. Mud Store 示例
- 4. Velocity模板語言(VTL): 介紹
- 5. Hello Velocity World!
- 6. 注釋
- 7. 引用
- 7.1. 變量Variables
- 7.2. 屬性
- 7.3. 方法
- 8. 形式引用符Formal Reference Notation
- 9. 安靜引用符Quiet Reference Notation
- 11. Case Substitution
- 12. 指令
- 12.1. #set
- 12.2. 字面字符串
- 12.3. 條件
- 12.3.1 If / ElseIf / Else
- 12.3.2 關系和邏輯操作符
- 12.4. 循環
- 12.4.1. Foreach 循環
- 12.5. 包含
- 12.6. 解析
- 12.7. 停止
- 12.10. 宏
- 12.10.1. Velocimacro 參數
- 12.10.2. Velocimacro 屬性
- 12.10.3. Velocimacro Trivia
- 13. Getting literal
- 13.1. 貨幣字符
- 13.2. 轉義 有效的 VTL 指令
- 13.3. 轉義 無效的 VTL 指令
- 14. VTL 格式化問題
- 15. 其它特征和雜項
- 15.1. 數學特征
- 15.2. 范圍操作符
- 15.3. 進階:轉義和!
- 15.4. Velocimacro 雜記
- 15.5. 字符串聯