《VTL語法參考指南》中文版[轉]

轉自:http://blog.csdn.net/javafound/archive/2007/05/14/1607935.aspx
html

VTL 語法參考指南》中文版
  聲明: 轉載請保留此頁聲明
**************************************************************************
此文檔爲藍傑實訓學員拓展實訓之用.
藍傑實訓不對譯文中某些說法可能會對您的系統或開發形成損害負責.
如對您有所幫助,咱們不勝榮幸!
*************************************************************************
本文屬NetJava.cn中的Velocity中文系列,本系包含以下文章:
《Velocity Java開發指南中文版》(Developer`s Guide)
《Velocity模板使用指南中文版》(User`s Guide)
《Velocity Web應用開發指南中文版》(Web Application Guide)
《VTL語法參考指南中文版》(VTL Reference)
《DB4O中文系列之起步篇》
 . . .
 更多資料請訪問http://www.netjava.cn/ 下載.
**************************************************************************
譯者: javaFound
*************************************************************************
Velocity(www.velocity.apache.org)一般用來替換JSP技術. 使用它生成頁面有如下優點:
·         簡潔通常的web美工不須要懂程序語言的就能夠設計動態業面.
·         Web系統容易維護 MVC推薦的作法是在頁面中不要存在其它的腳本語言出現..
·         容易訪問數據模型的命令和屬性頁面設計者經過引用簡單的就可訪問context中的java數據對象.
·         一致性 Velocity可用作其它的文本模板生成任務,如如發送email.
本系列全面講解了將Velocity應用從入門到精通其技術特色應用的每一個方面,助你成爲MVC構架的高手.
 
  
 
1.關於本指南
本文爲Velocity的模板語言參考書,如需瞭解更多信息,請參見 Velocity User Guide.
2.語法參考
1.變量定義
變量名的有效字符集:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]
Examples:
  • 通常方式: $mud-Slinger_9
  • 靜態(輸出原始字面): $!mud-Slinger_9
  • 正規格式: ${mud-Slinger_9}
2.訪問屬性
格式規則:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]
Examples:
  • 通常格式: $customer.Address :調用customer對象的getAddress()命令.
  • 正規格式: ${purchase.Total}
3.命令調用
格式規則:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ optional parameter list... ] ) [ } ]
Examples:
  • 通常寫碼: $customer.getAddress()
  • 正規寫法: ${purchase.getTotal()}
  • 傳入調用參數: $page.setTitle( "My Home Page" )
VTL的屬性調用能夠理解爲命令調用的簡寫方式,通常會調用對象的get/set命令.
3.動做指令
1.#set – 創建變量對值的引用
格式規則:
# [ { ] set [ } ] ( $ref = [ ", ' ]arg[ ", ' ] )
Examples:
  • 變量引用: #set( $monkey = $bill )
  • 引用原始字符串: #set( $monkey.Friend = 'monica' )
  • 屬性引用: #set( $monkey.Blame = $whitehouse.Leak )
  • 命令引用: #set( $monkey.Plan = $spindoctor.weave($web) )
直接引用數字: #set( $monkey.Number = 123 )
  • 列表賦值引用: #set( $monkey.Numbers = [1..3] )
  • 對象數組: #set( $monkey.Say = ["Not", $my, "fault"] )
右值也能夠作爲一個表達式出現,以下加,減,cheng,除和取模:
  • Addition: #set( $value = $foo + 1 )
  • Subtraction: #set( $value = $bar - 1 )
  • Multiplication: #set( $value = $foo * $bar )
  • Division: #set( $value = $foo / $bar )
  • Remainder: #set( $value = $foo % $bar )
2.#if/#elseif/#else-條件判斷
格式規則:
# [ { ] if [ } ] ( [條件表達式] ) [輸出內容] [ # [ { ] elseif [ } ] ( [condition] ) [output] ]* [ # [ { ] else [ } ] [output] ] # [ { ] end [ } ]
Usage:
  • condition – 若是是boolean型,根據true或false決定,不然非null時認爲是true.
  • output –能夠包含VTL的輸出內容.
Examples (showing different operators):
Symbol
Example
==
#if( $foo == 42 )
==
#if( $foo == "bar" )
==
#if( $foo == $bar )
!=
#if( $foo != $bar )
>
#if( $foo > 42 )
<
#if( $foo < 42 )
>=
#if( $foo >= 42 )
<=
#if( $foo <= 42 )
!
#if( !$foo )
注意:
  1. 「== 「操做能夠用來比較數字,字符串,或同一個類的不一樣對象或不一樣類型的對象. 當是不一樣類的對象時,會調用它們的toString()命令結果來作比較看是否相等.
  2. 也能夠以下用法,但注意else處,用{}括起.
#if( $foo == $bar)it's true!#{else}it's not!#end</li>
3.#foreach---使用循環經過列表迭代對象
Format:
# [ { ] foreach [ } ] ($refinarg)statement# [ { ] end [ } ]
Usage:
  • $ref – 引用的要迭代的對象.
  • arg – 多是:一個列表引用 (i.e. object array, collection, or map), an array list, 或其它列表.
  • statement – 當velocity發現下一個有效對像在列表中,輸出能夠是一個合法的VTL.
示例 #foreach()用法,:
  • 引用: #foreach ( $item in $items )
  • 數組列表: #foreach ( $item in ["Not", $my, "fault"] )
  • 根據設定的界限: #foreach ( $item in [1..3] )
以下能夠取得循環次數的當前值:
<table>
#foreach( $customer in $customerList )
    <tr><td>$velocityCount</td><td>$customer.Name</td></tr>
#end
</table>
默認的循環次數的引用變量名爲 $velocityCount. 能夠在配置文件velocity.properties中作以下修改爲你想要的:
# Default name of the loop counter
# variable reference.
directive.foreach.counter.name = velocityCount
 
# Default starting value of the loop
# counter variable reference.
directive.foreach.counter.initial.value = 1
注意,能夠對全部可循環的次數加一個最大值來控制,默認的是-1,表示元限制:
# The maximum allowed number of loops.
directive.foreach.maxloops = -1
4.#include – 在模板中引入本地文件,不用Velocity解析這個文件
Format:
# [ { ] include [ } ] ( arg[ arg2 ... argn] )
  • arg – 目錄TEMPLATE_ROOT下面的有效文件名.
Examples:
  • 直接寫文件名: #include( "disclaimer.txt,"opinion.txt" ):若有多個文件時用逗號分開
  • 使用變量引用的文件名: #include( $foo,$bar )
5.#parse – 在模板引用處使用Velocity解析另外一個模板輸出
Format:
# [ { ] parse [ } ] ( arg )
  • arg -目錄TEMPLATE_ROOT下面的有效文件名.
Examples:
  • 直接寫文件名: #parse( "lecorbusier.vm" )
  • 使用變量引用的文件名: #parse( $foo )
經過設置配置中的解析層次深度的最大值velocity.properties中項 parse_directive.maxdepth in能夠防止死循環. (The default parse depth is 10.)
6.#stop – 中斷模板解析
Format:
# [ { ] stop [ } ]
Usage:
在當前模板指令處中止解析,爲方便調試用.
7.#macro – 讓用戶能夠定義宏操做(Velocimacro (VM):一組實現特定功能的VTL)
Format:
# [ { ] macro [ } ] ( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] # [ { ] #end [ } ]
  • vmname – 宏名字 VM (#vmname)
  • $arg1 $arg2 [ ... ] – 要傳給宏的參數VM..
  • [ VM VTL code... ] –宏代碼,有效的VTL.
一次定義好了,就能夠在其它模板的任何地方使用宏指令來應用.
#vmname( $arg1 $arg2 )
宏(VM)能夠寫在如下兩個地方:
  1. (模板庫)Template library: 能夠配置用戶定義的庫以便全站使用
  2. Inline: 放入到通常的模板文件中, 僅當配置參數 velocimacro.permissions.allowInline=true 時生效.
4.Comments 註解
Comments不是運行時所必須的,但你必定要寫.
1.單行註解
Example:
## This is a comment.
2.多行註解
Example:
#*
This is a multiline comment.
This is the second line
*#
5.Feedback
相關文章
相關標籤/搜索