### 12.3.2 關系和邏輯操作符
Velocity 使用等式操作符來決定兩個變量間的關系。這里是一個簡單的例子演示如何使用等式操作符:
```
#set ($foo = "deoxyribonucleic acid")
#set ($bar = "ribonucleic acid")
#if ($foo == $bar)
In this case it's clear they aren't equivalent. So...
#else
They are not equivalent and this will be the output.
#end
```
Velocity 也具有邏輯AND, or 和 NOT 操作符。更進一步的信息,請看VTL參考手冊VTL Reference Guide 。下面是一些演示如何使用邏輯操作符的例子:
```
## logical AND
#if( $foo && $bar )
<strong> This AND that</strong>
#end
```
例子中\#if() 指令僅在$foo 和$bar 斗為真的時候才為真。如果$foo 為假,則表達式也為假;并且 $bar 將不被求值。如果 $foo 為真,Velocity 模板引擎將繼續檢查$bar;的值,如果 $bar 為真,則整個表達式為真。并且輸出This AND that 。如果 $bar 為假,將沒有輸出因為整個表達式為假。
邏輯OR 的工作方式相同,唯一的例外是其中一個表達式要被求值,以便決定整個表達式是否為真。請看下面的例子:
```
## logical or
#if( $foo || $bar )
<strong>This or That</strong>
#end
```
如果 $foo 為真,Velocity 模板引擎就不需要去察看$bar 的值,不管 $bar 是否為真,真個表達式都為真,因此輸出This or That 。如果 $foo 為假,$bar 就必須檢查其值了。在這種情況下,如果$bar 也是為假,表達式將為假,沒有任何輸出。當然,如果$bar 為真,則真個表達式為真,輸出This or That。
對于邏輯NOT 操作符,只有一個操作數:
```
##logical NOT
#if( !$foo )
<strong>NOT that</strong>
#end
```
這里,如果$foo 為真,!$foo 求值為假,沒有輸出。如果$foo 為假,!$foo 求值為真,輸出NOT that 。請當心,不要和安靜引用quiet reference $!foo 混淆它們是完全不同的。
- 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. 字符串聯