| |
| Main template markers/tags {variablename} 和大多的樣板系統一樣TemplatePower 使用 {variablename} 當成變數值 . 這個變數名稱必須是一個正數或者 是一個字串. 而字串不可包含 ( { ) 或空白. 像 {-1}, {a{name} or {a name} 都是不合法的設定. 而你可以使用 assign()函數來控制變數 . 所有變數標幟, 及區塊, 而一個變數只被視為一個變數. 所以, 他只接受一個變數值.重覆則被取代. | <!-- START BLOCK : blockname --> <!-- END BLOCK : blockname --> |
你可以使用動態區塊,例如,你要建立一個多行的產器清單之類的. 只需要把樣板語法方在 start/end 標幟之間 . 注意標幟不可換行,才能正常的執行. 而且你必須要給他一個區塊名稱:blockname (.).
| <!-- INCLUDE BLOCK : iblockname --> |
你可以在你的樣板中插入其他的樣板 (包含樣板自己, 沒有物件的), HTML網頁,及其他. 但是如果你插入一個 PHP-程式碼他是不會被執行的. 如果你想要讓他可以被執行請使用 includescript 區塊的標幟, 在v2.0版之後就加入了這樣的選項. 在使用上請不要換行, 否則程式將無法執行.區 塊名稱命名請使用字串和變數設定規則一樣 這個區塊必須使用 assignInclude()來控制. 在3.0版也支援這樣的格式運作. <!-- INCLUDE BLOCK : ./header.tpl -->
|
在2.0版後可插入執行後的內容 | <!-- INCLUDESCRIPT BLOCK : iblockname --> |
這樣可以直接插入php的程式碼. 您可以使用 assignInclude()來控制這個插入區塊. 在3.0之後的版本支援直接插入檔案的方式: | <!-- INCLUDESCRIPT BLOCK : ./header.php --> |
| <!-- START IGNORE --> <!-- END IGNORE --> |
在這個標幟中間的內容將會被省略. <!-- REUSE BLOCK : orig_bname AS copy_bname --> |
|
複製一個區塊成不同 的名稱. 方便除錯使用. 他只會在沒有使用子區塊的情況下正常運作 使用資料庫的方式來存 放樣板! 請直接參閱使用方式. |
|
| |
適用版本:All Versions 建立一個 TemplatePower 物件 說明: object TemplatePower ( string basefile/content, [const type] ) 預設格式的參數為 T_BYFILE , 指使用檔案讀入的方式. 如果你要直接傳入內容必須要設定參數為 T_BYVAR. 範例: | <?php //create a new TemplatePower object using a file $tpl = new TemplatePower ( "./base.tpl" ); $tpl = new TemplatePower ( "./base.tpl" , T_BYFILE ); //create a new TemplatePower object using a variable $tpl = new TemplatePower ( $base_content , T_BYVAR ); //create a new TemplatePower object using a //serialized template $tpl = new TemplatePower ( $base_content ); $tpl -> serializedBase (); ?> |
1.62版只支援檔案讀取方式. 在v.20中 才加入第二個選項, 揩定樣板格式為一個連續的檔案. 在 3.0+ 使用 serializedBase() 取代這個功能了. 範例 v2.0 物件. | <?php //create a new TemplatePower object using a file $tpl = new TemplatePower ( "./base.tpl" ); //create a new TemplatePower object using a //serialized template $tpl = new TemplatePower ( "./base.stpl" , true ); ?> |
|
|
| |
適用版本:All Versions 寫入變數到指定變數區塊 說明: assign ( string [blockname.]variablename, mixed value ) assign ( Array( [blockname.]variablename => value ) ) 預設值為 空字串 參考: assignGlobal(), getVarValue(), showUnAssigned(). 範例 1. simple.tpl | <html> <head> <title>Simple Template Example</title> </head> <body> <H2>Welcome {name}!</H2> </body> </html> |
myscript.php | <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower ( "./simple.tpl" ); $tpl -> prepare (); $tpl -> assign ( "name" , "Ron" ); $tpl -> printToScreen (); ?> |
寫入函式: | <?php $tpl->assign( "name", "Ron" ); $tpl->assign( Array( product_id => $pid, productname => $pname )); ?> |
變數和區塊 變數和區塊為父子關係. 範例1, 是未指定區塊中的變數名稱. 本身區塊預設的名稱 為 _ROOT , 而你可以指定的區塊中的變數值. 所以你可以以父子關係來指定值 'blockname.variablename'. | php程式碼: |
| <?php $tpl -> assign ( "_ROOT.name" , "Ron" ); $tpl -> assign ( "book.title" , "Core PHP Programming" ); ?> |
|
|
| |
適用版本:(TP 2.0+) 變數指定全域化 說明: assignGlobal ( string variablename, mixed value ) assignGlobal ( array( variablename => value ) ) assignGlobal() 函數指定一個預設值給所有的變數, 包含所有子變數,給指定的名稱, 如果你只是要指定給單一變數可使用 assign(). 範例 1.
img.tpl | <html> <head> <title>AssignGlobal Example</title> </head> <body> <img src="{imagedir}/logo.gif"> <!-- START BLOCK : image --> <img src="{imagedir}/img_{id}.gif"> <!-- END BLOCK : image --> </body> </html> |
myscript.php | php程式碼: |
| <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower ( "./img.tpl" ); $tpl -> prepare (); $tpl -> assignGlobal ( "imagedir" , "images" ); for ( $i = 1 ; $i <= 10 ; $i ++ ) { $tpl -> newBlock ( "image" ); $tpl -> assign ( "id" , $i ); } $tpl -> printToScreen (); ?> |
範例2 | php程式碼: |
| <?php $tpl -> assignGlobal ( "basedir" , "/usr/local/apache/www/" ); $tpl -> assignGlobal ( Array( basedir => "/usr/local/apache/www/" , imagedir => "images/" )); ?> |
|
|
| |
適用版本:(TP 2.0+) 插入檔案或資料 說明 assignInclude ( string includename, string filename/content, [const type] ) 預設格式的參數為 T_BYFILE , 指使用檔案讀入的方式. 如果你要直接傳入內容必須要設定參數為 T_BYVAR. 範例 1. include.tpl | <html> <head> <title>AssignInclude Example</title> </head> <body> <!-- INCLUDE BLOCK : header --> <!-- INCLUDESCRIPT BLOCK : content --> </body> </html> |
myscript.php | <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower ( "./include.tpl" ); $tpl -> assignInclude ( "header" , "./header.tpl" ); $tpl -> assignInclude ( "content" , "./about.php" ); $tpl -> prepare (); $tpl -> printToScreen (); ?> |
| Examples of the assignInclude function $tpl->assignInclude( "header", "./header.tpl" ); $tpl->assignInclude( "content", $content, T_BYVAR ); |
使用資料庫插入 | <?php include( "./class.TemplatePower.inc.php" ); //connect to database $link = mysql_connect ( "host" , "user" , "passwd" ) or die( "Could not connect" ); mysql_select_db ( "my_database" ) or die( "Could not select database" ); //get database templates $qry = "SELECT base, header FROM templates" ; $result = mysql_query ( $qry ); if( mysql_num_rows ( $result ) > 0 ) { list( $base , $header ) = mysql_fetch_row ( $result ); } //make a new TemplatePower object $tpl = new TemplatePower ( $base , T_BYVAR ); //assign include template by variable $tpl -> assignInclude ( "header" , $header , T_BYVAR ); $tpl -> prepare (); //print the result $tpl -> printToScreen (); ?> |
|
|
| |
適用版本:(unknown) 回傳一個變數值 說明: getVarValue ( string [blockname.]variablename ) 你只可以取得最後一個變數的值. 這表示你不能取得前二次的變數, 你只能取得前一次的變數或取塊值. 範例 1. number.tpl | <html> <head> <title>AssignInclude Example</title> </head> <body> <!-- START BLOCK : number --> {number} <!-- END BLOCK : number --> {total} </body> </html> |
myscript.php | <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower ( "./number.tpl" ); $tpl -> prepare (); for( $i = 1 ; $i <= 10 ; $i ++ ) { $tpl -> newBlock ( "number" ); $tpl -> assign ( "number" , $i ); $tpl -> assign ( "_ROOT.total" , ( $tpl -> getVarValue ( "_ROOT.total" ) + $i ) ); } $tpl -> printToScreen (); ?> |
|
|
| |
適用版本:(unknown) 移 'blockpointer'(區塊指標) 到某一個區塊, 讓他成為目前使用的區塊 說明 gotoBlock ( string blockname ) 移 'blockpointer'(區塊指標) 某一個區塊, 讓他成為目前使用的區塊. 這很方便您可以回到上一次寫入 的區塊. newBlock.tpl | <html> <head> <title>NewBlock</title> </head> <body> <table> <tr><td>Names</td></tr> <!-- START BLOCK : name_row --> <tr> <td>{name}</td> </tr> <!-- END BLOCK : name_row --> </table> <br> {total_names} </body> </html> |
myscript.php | <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower ( "./newBlock.tpl" ); $tpl -> prepare (); $count = 0 ; while( $count < 10 ) { $tpl -> newBlock ( "name_row" ); $tpl -> assign ( "name" , "Ron" ); $count ++; } $tpl -> gotoBlock ( "_ROOT" ); $tpl -> assign ( "total_names" , $count ); $tpl -> printToScreen (); ?> |
|
|
| |
適用版本:(unknown) 建立一個新的區塊 說明 newBlock ( string blockname ) 在使用 newBlock() TemplatePower 將設定 'blockpointer'(區塊指標) 到這一個使用的區塊名稱 .所以你如果要指定使用其他的區塊, 你必須移動 'blockpointer'(區塊指標) 到指定的區塊 才可以進行區塊工作. newBlock.tpl | <html> <head> <title>NewBlock</title> </head> <body> <table> <tr><td>Names</td></tr> <!-- START BLOCK : name_row --> <tr> <td>{name}</td> </tr> <!-- END BLOCK : name_row --> </table> <br> {total_names} </body> </html> |
myscript.php | <?php include_once( "./class.TemplatePower.inc.php" ); $tpl = new TemplatePower ( "./newBlock.tpl" ); $tpl -> prepare (); $count = 0 ; while( $count < 10 ) { $tpl -> newBlock ( "name_row" ); $tpl -> assign ( "name" , "Ron" ); $count ++; } $tpl -> gotoBlock ( "_ROOT" ); $tpl -> assign ( "total_names" , $count ); $tpl -> printToScreen (); ?> |
如果你沒有使用 newBlock(),區塊將被隱藏! form.tpl | <html> <head> <title></title> </head> <body> <!-- START BLOCK : error --> The following errors occurred.<br> <!-- START BLOCK : message --> - {message}<br> <!-- END BLOCK : message --> <!-- END BLOCK : error --> <form method="post" action="myscript.php"> Email: <input type="text" name="email"> <input type="submit" name="submit" value="Submit"> </form> </body> </html> |
myscript.php | <?php include_once( './class.TemplatePower.inc.php' ); $tpl = new TemplatePower ( 'form.tpl' ); $tpl -> prepare (); $errorMessage = Array(); $errorFound = false ; if( isset( $submit ) ) { if( $email == '' ) { $errorMessage [] = 'No emailadress entered' ; $errorFound = true ; } if( $errorFound ) { $tpl -> newBlock ( 'error' ); $size = sizeof ( $errorMessage ); for( $i = 0 ; $i < $size ; $i ++ ) { $tpl -> newBlock ( 'message' ); $tpl -> assign ( 'message' , $errorMessage [ $i ]); } } else { Header ( 'Location: member.php' ); } } $tpl -> printToScreen (); ?> |
|
|
| |
| 適用版本:(unknown) 解析樣板 說明: prepare () 此函數會對您的樣板進行解析! 形成樣板物件 ,才可以使用! |
|
| |
| 適用版本:(unknown) 輸出解析完的樣板結果 說明: printToScreen () |
|
| |
| 適用版本:(TP 3.0+) 告訢樣板使用檔案格式或是用資料格式 說明: serializedBase () |
|
| |
| 適用版本:(TP 3.0+) 設定是否秀出未使用的變數 說明: showUnAssigned ( bool state ) 當參數state為TRUE時則代表秀出未使用的變數 |
|
| |
| 適用版本:(TP 2.0+) 傳回目前使用 TemplatePower的版本 說明: version () |
|
| |
| 適用版本:(unknown) 回傳輸出內容 說明 getOutputContent () |
|