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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

VTL语法

發布時間:2023/12/14 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VTL语法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、引用

1.變量

格式:
$ [ ! ][ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ][ } ]

舉例:
Normal 格式: $mud-Slinger_9
Silent 格式: $!mud-Slinger_9
Formal 格式: ${mud-Slinger_9}

2.屬性

格式:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[a..z, A..Z ][ a..z, A-Z, 0..9, -, _ ]* [ } ]

舉例:
Regular 格式: $customer.Address
Formal 格式: ${purchase.Total}

3.方法

格式:
$ [ { ][ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]* .[ a..z, A..Z ][ a..z, A..Z, 0..9, -, _ ]*( [ opional parameter list... ] ) [ } ]

舉例:
Regular 格式: $customer.getAddress()
Formal 格式: ${purchase.getTotal()}
Regular 格式 with Parameter List: $page.setTitle( "My Home Page" )

二、指令

1.#set - 給引用賦值

格式:
#set( $ref = [ ", ' ]arg[ ", ' ] )

用法:
$ref - 左邊必須是一個變量引用或者屬性引用.
arg - 右邊的arg如果被加上雙引號則被解析, 如果是單引號則不會解析. 如果右邊的值為null,則應 該不會賦值給左邊

舉例:
變量引用: #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"] )
右邊也可以是簡單算術表達式, 例如:

加: #set( $value = $foo + 1 )
減: #set( $value = $bar - 1 )
乘: #set( $value = $foo * $bar )
除: #set( $value = $foo / $bar )
求余: #set( $value = $foo % $bar )

2.#if / #elseif / #else - output conditional on truth of statements

格式:
#if( [condition] ) [output] [ #elseif( [condition] ) [output] ]* [ #else [output] ] #end

用法:
condition - If a boolean, considered true if it has a true false; if not a boolean, considered true if not null.
output - May contain VTL.

舉例:
Equivalent Operator: #if( $foo == $bar )
Greater Than: #if( $foo > 42 )
Less Than: #if( $foo < 42 )
Greater Than or Equal To: #if( $foo >= 42 )
Less Than or Equal To: #if( $foo <= 42 )
Equals Number: #if( $foo == 42 )
Equals String: #if( $foo == "bar" )
Boolean NOT: #if( !$foo )

3.#foreach - 循環列值

格式:
#foreach( $ref in arg ) statement #end

用法:
$ref - 第一個變量引用.
arg - 可以是object array, collection, or map的引用。
statement -

引用: #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>

4. 默認計數器

在velocity.properties文件了有一個默認的循環計數器, 它是$velocityCount. 默認是從1開始計數,但是這個計數器的初值可以在velocity.properties文件里設置. 在velocity.properties如下設置:

# Default name of the loop counter
# refenence refernce.
counter.name = velocityCount

# Default starting value of the loop
# counter 變量引用.
counter.initial.value = 1

5.#include - 不被Velocity解析

格式:
#include( arg[, arg2, ... argn] )

arg -文件名

舉例:
字符串: #include( "disclaimer.txt", "opinion.txt" )
變量: #include( $foo, $bar )

6.#parse - 被Velocity解析

格式:
#parse( arg )
arg - 文件名

舉例:
字符串: #parse( "lecorbusier.vm" )
變量: #parse( $foo )

7.#stop - 停止模板引擎

格式:
#stop

用法:
將會停止當前模板引擎的執行,這是一個很好的調試。

8.#macro - 定義一個Velocity宏

所有的用戶都可以定義一個Velocity宏,允許模板設計者定義一段可重用的VTL template
格式:
#macro( vmname $arg1 [ $arg2 $arg3 ... $argn ] ) [ VM VTL code... ] #end
例如:
#macro ( d )
<tr><td></td></tr>
#end
在上面的例子中Velocimacro被定義為d,然后你就可以在任何VTL directive中以如下方式調用它:
#d()
當你的template被調用時,Velocity將用<tr><td></td></tr>替換為#d()。

例如2:帶參數的宏
#macro ( tablerows $color $somelist )
#foreach ( $something in $somelist )
<tr><td bgcolor=$color>$something</td</tr>
#end
#end
調用#tablerows Velocimacro:
#set ( $greatlakes = [ “Superior”, “Michigan”, “Huron”, “Erie”, “Ontario” ] )
#set ( $color = “blue” )
<table>
#tablerows( $color $greatlakes )
</table>
經過以上的調用將產生如下的顯示結果:
<table>
<tr><td bgcolor=” blue”> Superior </td></tr>
<tr><td bgcolor=” blue”> Michigan </td></tr>
<tr><td bgcolor=” blue”> Huron </td></tr>
<tr><td bgcolor=” blue”> Erie </td></tr>
<tr><td bgcolor=” blue”> Ontario </td></tr>
</table>

三、注釋

1. 單行注釋 ##

例如:
## This is a comment.

2. 多行注釋

Example:
#*
This is a multiline comment.
This is the second line
*#

轉載于:https://www.cnblogs.com/zhuhc/archive/2013/05/04/3059489.html

總結

以上是生活随笔為你收集整理的VTL语法的全部內容,希望文章能夠幫你解決所遇到的問題。

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