1. Velocity 是什麼?
Velocity 是一個基於Java的模版引擎,它提供模版語言用於引用Java程式碼定義的物件。
Velocity能建立HTML頁面,並預留佔位符。 (最基本用途)
Velocity能基於範本產生Java,SQL原始碼。 (沒看過)
自動產生電子郵件。 (沒看過)
讀取XML,並轉換成所需的文件格式,通常是HTML。 (沒看過)
#單行
##多行#* *
#程式碼區塊註解(訊息) ##* *
# 定義:
# ##set( $foo = "Velocity" )
$foo ${foo}
#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )
#if( $foo < 10 ) <strong>Go North</strong> #elseif( $foo == 10 ) <strong>Go East</strong> #else <strong>Go West</strong> #end
#foreach( $customer in $customerList ) #if( $foreach.count > 5 ) #break #end $customer.Name #end
#include( "one.vm" ) one.vm 不解析。 #parse( "me.vm" ) me.vm 解析。
#define( $block ) Hello $who #end #set( $who = 'World!' ) $block
#macro( d ) <tr><td></td></tr> #end #d()
#macro( d ) <tr><td>$!bodyContent</td></tr> #end #@d()Hello#end
#定義
#macro( tablerows $color $somelist ) #foreach( $something in $somelist ) <tr><td bgcolor=$color>$something</td></tr> #end #end
呼叫
#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] ) #set( $color = "blue" )
<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>
$foo ## is the same as ${foo} $foo.getBar() ## is the same as $foo.Bar$data.setUser("jon") ## is the same as #set( $data.User = "jon" ) $data.getRequest().getServerName() ## is the same as $data.Request.ServerName## is the same as ${data.Request.ServerName}
2. Velocity 變數未定義
$!
,例######$!foo
getaddress()getAddress()get("address")isAddress()
以上是Velocity實例教學分享的詳細內容。更多資訊請關注PHP中文網其他相關文章!