日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Velocity模板语言(VTL):说明

發布時間:2023/12/14 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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正常顯示.你需要用!emailemail.所以上面的例子會改成下面

非正式

<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

include允許模板設計者引入本地文件,被引入文件的內容將不會通過模板引擎被render.為了安全的原因,被引入的本地文件只能在TEMPLATE_ROOT目錄下.

include( “one.gif”, “two.txt”, “three.txt” ) ## 多個文件用’,’分隔

parse

就像#include,#parse接受一個變量而不是模板,任何由#parse指向的模板都必須包含在TEMPLATE_ROOT目錄下.與#include不同的是,#parse只能指定單個對象

#stop

stop 允許模板設計者停止執行模板引擎并返回,把它應用于debug是很有幫助的.

Velocimacros

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):说明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。