Velocity模板语言(VTL):说明
VTL statement
語法部分
directive(指令)
#set( $a = "Velocity" ) // 賦值語句在上面的例子中,#set是分配一個值給變量.變量$a在模板中輸出
- Velocity中僅有String可以被賦值給變量
- 使用$字符開始的references用于得到什么;使用#字符開始的directives用于作些什么.
Hello Velocity World!
<html><body>#set( $foo = "Velocity" )Hello $foo World!</body> </html>注釋
## This is a sing line comment.#* Thus begins a multi-line comment. *##** This is a VTL comment block and may be used to store such information as the document author and versioning information: @author @version *#References
在VTL中有三種類型References: 變量(variables),屬性(properties),方法(methods).
- 所有的reference被作為一個String對象處理.如果有一個對象$foo是一個Integer對象,那么Velocity將調用它的toString()方法將這個對象轉型為String類型.
變量(variables)
非正式變量是由’$’開關,接著是VTL標識符.VTL標識符必須以字母(a-z|A-Z)開關,剩下的部分限于以下幾種: (a-z|A-Z|0-9|-|_)
:例: costmer?##?非正式{costmer} ## 正式
屬性(properties)
非正式變量是由’$’開頭,接著是VTL標識符,再接著就是字符(“.”)和其他的VTL標簽符.
:例: costomer.Address?##?非正式{costomer.Address} ## 正式
上面的例子有兩種含義.它可以表示:查找hashtable對象customer中以Address為關鍵字的值;也可以表示調用customer對象的getAddress()方法.當你的頁面被請求時,Velocity將確定以上兩種方式選用哪種,然后返回適當的值.
方法(methods)
非正式方法是由’$’開始并跟隨VTL標識符組成的References,一般還包括一個VTL方法體.一個方法體包括一個VTL標識接著一個左手號(‘(‘),接著是參數列表,再接著是右括號(‘)’).
這里是一些在VTL中有效的方法定義:
:例: customer.getAddress()?##?非正式page.setTitle( “My Home Page!” ) ## 非正式
{customer.getAddress()}?##?正式{customer.setTitle()} ## 正式
Quiet reference notation
<input type="text" name="email" value="$email"/>當form最初加載的時候,變量email沒有值,但你想出現一個空白的文本框設定值為"email”.使用quiet reference notation可以使Velocity正常顯示.你需要用!email來代替email.所以上面的例子會改成下面
非正式
<input type="text" name="email" value="$!email"/>正式
<input type="text" name="email" value="$!{email}"/>Escaping valid VTL reference (逃避特殊符號)
#set( $email = "foo" ) $email \$email \\$email \\\$email將顯示為
foo
$email
\foo
\$email
Case substitution
$foo.getBar() ## is the same as $foo.Bar$data.getUser("jon") ## is the same as $data.User("jon)Directives
#set
#set( $primate = "monkey" ) #set( $customer.Behavior = "$primate" )- 左邊一定是一個變量或者一個屬性
- 右邊可以是(變量,字符串,屬性,方法,數字,數組,算術表達式)
- 如果右邊是null,VTL的處理將比較特殊,它將指向一個已經存在的reference
條件語句 (if/elseif/else/end)
#if( $foo && $bar)<strong> This and that</strong> #end循環 foreach
#foreach( $product in $allProducts )<li>$product</li> #end- $allProducts可以 是珍上Vector,Hashtable,Array
- 默認循環索引變量 $velocityCount可以在velocity.properties設置
include允許模板設計者引入本地文件,被引入文件的內容將不會通過模板引擎被render.為了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目錄下.
include( “one.gif”, “two.txt”, “three.txt” ) ## 多個文件用’,’分隔
就像#include,#parse接受一個變量而不是模板,任何由#parse指向的模板都必須包含在TEMPLATE_ROOT目錄下.與#include不同的是,#parse只能指定單個對象
stop 允許模板設計者停止執行模板引擎并返回,把它應用于debug是很有幫助的.
macro允許定義一段可征用的VTL template.
#macro( d )<tr><td></td></tr> #end#d()
- Velocimacro arguments (Reference, String literal, Number literal, IntegerRange[1..3],對象數組,boolean值)
Velocimacro properties
- velocimacro.library: 一個逗號分隔的模板庫列表.默認情況下velocity查找唯一的一個庫: VM_global_library.vm.
- velocimacro.permissions.allow.inline屬性:有兩個可靠的值true或者false,通過它可以確定Velocimacros是否可以被定義在regular template內.默認值是true.
- velocimacro.permissions.allow.inline.replace.global屬性有兩個可靠的值true或者false,通過它可以確定inline的Velocimacro定義是否可以替代全局的Velocimacro定義
- velocimacro.permissions.allow.inline.local.scale屬性有兩個可靠的值true或者false,默認false.作用是用于確定inline定義的velocimacros是否僅僅補定義的template內可見
- velocimacro.context.localscope屬性有true和false兩個可選值,默認值為false.當設置為true時,任何在Velocimacro內通過#set()對context的修改被認為是針對此velocimacro的本地設置,而不會永久的影響內容.
- velocimacro.library.autoreload屬性控制velocimacro庫的自動加載.默認是false,可以配置為開發模式.
附: 開發環境做以下配置,可避免修改VTL重啟容器
file.resource.loader.path = templates file.resource.loader.cache = false velocimacro.library.autoreload = true總結
以上是生活随笔為你收集整理的Velocity模板语言(VTL):说明的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据是什么?一篇文章正确告诉你
- 下一篇: linux心跳出血漏洞,heartble