模板引擎 Velocity语法
Velocity是一個基于java的模板引擎(template engine),它允許任何人僅僅簡單的使用模板語言(template language)來引用
由java代碼定義的對象。作為一個比較完善的模板引擎,Velocity的功能是比較強大的,但強大的同時也增加了應用復雜性。
基本語法
1、"#“用來標識Velocity的腳本語句,包括#set、#if 、#else、#end、#foreach、#end、#include、#parse、#macro等;
如:
#if(info.imgs)<imgsrc="info.imgs) <img src="info.imgs)<imgsrc="info.imgs” border=0>
#else
#end
2、""用來標識一個對象(或理解為變量);如如:"用來標識一個對象(或理解為變量);如 如:"用來標識一個對象(或理解為變量);如如:i、msg、msg、msg、TagUtil.options(…)等。
3、"{}"用來明確標識Velocity變量;
比如在頁面中,頁面中有一個someonename,此時,Velocity將把someonename作為變量名,若我們程序是想在someone這個變量的后面緊接著顯示name字符,則上面的標簽應該改成someonename,此時,Velocity將把someonename作為變量名,若我們程序是想在someone這個變量的后面緊接著顯示name字符,則上面的標簽應該改成someonename,此時,Velocity將把someonename作為變量名,若我們程序是想在someone這個變量的后面緊接著顯示name字符,則上面的標簽應該改成{someone}name。
4、"!"用來強制把不存在的變量顯示為空白。
如當頁面中包含msg,如果msg對象有值,將顯示msg的值,如果不存在msg對象同,則在頁面中將顯示msg,如果msg對象有值,將顯示msg的值,如果不存在msg對象同,則在頁面中將顯示msg,如果msg對象有值,將顯示msg的值,如果不存在msg對象同,則在頁面中將顯示msg字符。這是我們不希望的,為了把不存在的變量或變量值為null的對象顯示為空白,則只需要在變量名前加一個“!”號即可。
如:!msg我們提供了五條基本的模板腳本語句,基本上就能滿足所有應用模板的要求。這四條模板語句很簡單,可以直接由界面設計人員來添加。在當前很多EasyJWeb的應用實踐中,我們看到,所有界面模板中歸納起來只有下面四種簡單模板腳本語句即可實現:1、!msg 我們提供了五條基本的模板腳本語句,基本上就能滿足所有應用模板的要求。這四條模板語句很簡單,可以直接由界面設計人員來添加。在當前很多EasyJWeb的應用實踐中,我們看到,所有界面模板中歸納起來只有下面四種簡單模板腳本語句即可實現: 1、!msg我們提供了五條基本的模板腳本語句,基本上就能滿足所有應用模板的要求。這四條模板語句很簡單,可以直接由界面設計人員來添加。在當前很多EasyJWeb的應用實踐中,我們看到,所有界面模板中歸納起來只有下面四種簡單模板腳本語句即可實現: 1、!obj 直接返回對象結果。
如:在html標簽中顯示java對象msg的值。
!msg</p>在html標簽中顯示經過HtmlUtil對象處理過后的msg對象的值<p>!msg</p> 在html標簽中顯示經過HtmlUtil對象處理過后的msg對象的值 <p>!msg</p> 在html標簽中顯示經過HtmlUtil對象處理過后的msg對象的值 <p>!HtmlUtil.doSomething($!msg)
2、#if(KaTeX parse error: Expected 'EOF', got '#' at position 7: !obj) #?else #end 判斷語句 …msg)
#end
上面的腳本表示當對象msg對象存在時,輸出
3、#foreach( $info in $list) $info.someList #end 循環讀取集合list中的對象,并作相應的處理。
如:EasyJF開源論壇系統中論(0.3)壇首頁顯示熱門主題的html界面模板腳本:
#foreach( $info in KaTeX parse error: Expected 'EOF', got '&' at position 52: …WebCommand=show&?&cid=!info.cid" target="_blank">$!info.title
#end
上面的腳本表示循環遍歷hotList1集合中的對象,并輸出對象的相關內容。
4、#macro(macroName)#end 腳本函數(宏)調用,不推薦在界面模板中大量使用。
如:在使用EasyJWeb Tools快速生成的添刪改查示例中,可以點擊列表的標題欄進行升降排序顯示,這是我們在EasyJWeb應用中經常看到的一個排序狀態顯示的模板內容。
函數(宏)定義,一般放在最前面
#macro(orderPic KaTeX parse error: Expected 'EOF', got '#' at position 10: type) #?if (orderField.equals(type))<imgsrc="/images/ico/type)) <img src="/images/ico/type)) <imgsrc="/images/ico/{orderType}.gif">
#end
#end
具體的調用如:頭銜#orderPic(“title”)
5、包含文件#inclue(“模板文件名”)或#parse(“模板文件名”)
主要用于處理具有相同內容的頁面,比如每個網站的頂部或尾部內容。
使用方法,可以參考EasyJF開源Blog及EasyJF開源論壇中的應用!
如:#parse("/blog/top.html")或#include("/blog/top.html")
parse與include的區別在于,若包含的文件中有Velocity腳本標簽,將會進一步解析,而include將原樣顯示。
關于#set的使用
在萬不得已的時候,不要在頁面視圖自己聲明Velocity腳本變量,也就是盡量少使用#set。有時候我們需要在頁面中顯示序號,而程序對象中又沒有包含這個序號屬性同,可以自己定義。如在一個循環體系中,如下所示:
#set (KaTeX parse error: Expected 'EOF', got '#' at position 8: i=0) #?foreach(info in list)序號:list) 序號:list) 序號:i
#set(i=i=i=i+1)
#end
Velocity腳本語法摘要
1、聲明:#set (KaTeX parse error: Expected 'EOF', got '#' at position 118: …Number literal #?set (i=1)
ArrayList #set ($arr=[“yt1”,“t2”])
算術運算符
2、注釋:
單行## XXX
多行#* xxx
xxxx
xxxxxxxxxxxx*#
References 引用的類型
3、變量 Variables
以 “$” 開頭,第一個字符必須為字母。character followed by a VTL Identifier. (a … z or A … Z).
變量可以包含的字符有以下內容:
alphabetic (a … z, A … Z)
numeric (0 … 9)
hyphen ("-")
underscore ("_")
4、Properties
Identifier.IdentifierIdentifier.Identifier Identifier.Identifier user.name
hashtable user中的的name值.類似:user.get(“name”)
5、Methods
object user.getName() = $user.getName()
6、Formal Reference Notation
用{}把變量名跟字符串分開
如
#set (KaTeX parse error: Expected 'EOF', got '}' at position 11: user="csy"}? {user}name
返回csyname
usernameusername username !username
與與與!的區別
當找不到username的時候,username返回字符串"username返回字符串"username返回字符串"username",而$!username返回空字符串""
7、雙引號 與 引號
#set (var="helo")test"var="helo") test"var="helo") test"var" 返回testhello
test’var′返回test′var' 返回test'var′返回test′var’
可以通過設置 stringliterals.interpolate=false改變默認處理方式
8、條件語句
#if( KaTeX parse error: Expected 'EOF', got '#' at position 39: …ty!</strong> #?end #if(foo)
#elseif()
#else
#end
當$foo為null或為Boolean對象的false值執行.
9、邏輯運算符:== && || !
10、循環語句#foreach($var in $arrays ) // 集合包含下面三種Vector, a Hashtable or an Array
#end
#foreach( $product in allProducts)<li>allProducts ) <li>allProducts) <li>product
#end
#foreach( $key in $allProducts.keySet() )
- Key: $key -> Value: allProducts.get(allProducts.get(allProducts.get(key)
#end#foreach( $customer in customerList)<tr><td>customerList ) <tr><td>customerList) <tr><td>velocityCount$customer.Name
#end11、velocityCount變量在配置文件中定義
# 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 = 112、包含文件
#include( “one.gif”,“two.txt”,“three.htm” )13、Parse導入腳本
#parse(“me.vm” )14、#stop 停止執行并返回
15、定義宏Velocimacros ,相當于函數 支持包含功能
#macro( d )
#end
調用
#d()16、帶參數的宏
#macro( tablerows $color $somelist )
#foreach( $something in somelist)<tr><tdbgcolor=somelist ) <tr><td bgcolor=somelist) <tr><tdbgcolor=color>$something
#end
#end17、Range Operator
#foreach( $foo in [1…5] )
總結
以上是生活随笔為你收集整理的模板引擎 Velocity语法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第十二期:七种优秀的浏览器兼容性测试工具
- 下一篇: spring mvc学习(27):处理数