生活随笔
收集整理的這篇文章主要介紹了
《VTL语法参考指南》中文版[转]
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自:http://blog.csdn.net/javafound/archive/2007/05/14/1607935.aspx
《 VTL 語法參考指南》中文版 源文見 http://velocity.apache.org 聲明: 轉載請保留此頁聲明 ************************************************************************** 此文檔為藍杰實訓 學員拓展實訓之用. 藍杰實訓 不對譯文中某些說法可能會對您的系統或開發造成損害負責. 如對您有所幫助,我們不勝榮幸! ************************************************************************* 本文屬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 Mail: javafound@gmail.com NetJava.cn@gmail.com ************************************************************************* Velocity(www.velocity.apache.org) 通常用來替換JSP 技術. 使用它生成頁面有以下優勢: ·???????? 簡潔 –一般的web 美工不需要懂程序語言的就可以設計動態業面. ·???????? Web 系統容易維護 – MVC 推薦的做法是在頁面中不要存在其它的腳本語言出現.. ·???????? 容易訪問數據模型的命令和屬性 –頁面設計者通過引用簡單的就可訪問context 中的java 數據對象. ·???????? 一致性 – Velocity 可用做其它的文本模板生成任務,如如發送email. 本系列全面講解了將Velocity應用從入門到精通其技術特點應用的每個方面,助你成為MVC構架的高手. 目 ?? 錄 1.關于本指南 2.語法參考 1.變量定義 2.訪問屬性 命令調用 3.動作指令 1.#set – 建立變量對值的引用 2.#if/#elseif/#else-條件判斷 3.#foreach---使用循環通過列表迭代對象 4.#include – 在模板中引入本地文件,不用Velocity解析這個文件 5.#parse – 在模板引用處使用Velocity解析另一個模板輸出 6.#stop – 中斷模板解析 7.#macro – 讓用戶可以定義宏操作(Velocimacro (VM):一組實現特定功能的VTL) 4.Comments 注解 1.單行注解 2.多行注解 5.Feedback 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 )
注意: “== “操作可以用來比較數字,字符串,或同一個類的不同對象或不同類型的對象. 當是不同類的對象時,會調用它們的toString()命令結果來做比較看是否相等. 也可以如下用法,但注意else處,用{}括起. #if( $foo == $bar)it's true!#{else}it's not!#end</li> 3.#foreach---使用循環通過列表迭代對象 Format: # [ { ] foreach [ } ] ( $ref in arg ) 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)可以寫在以下兩個地方: (模板庫)Template library: 可以配置用戶定義的庫以便全站使用 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
轉載于:https://www.cnblogs.com/vincent-blog/p/4477207.html
總結
以上是生活随笔 為你收集整理的《VTL语法参考指南》中文版[转] 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。