SmartTemplate學(xué)習(xí)筆記
目錄
1、SmartTemplate的效率
2、基本變量
3、邏輯運(yùn)算結(jié)構(gòu)
4、模式(Methods)
5、擴(kuò)展類(Extensions) 未完成
------------------------------------------------------------------------
1、SmartTemplate的效率
雖然他有很多的程序來(lái)形成強(qiáng)大的功能,但在執(zhí)行時(shí)只有你調(diào)用的才被導(dǎo)入,所以不用擔(dān)心這方面的速度影響,同樣這套模版系統(tǒng)是為最快的執(zhí)行效率而優(yōu)化過(guò)的,比起目前市場(chǎng)上常見(jiàn)的Smarty,要快不少(Smarty采用后期緩存,所以比較可能不是很準(zhǔn)確)。
2、SmartTemplate的變量
Array的變量是由SmartTemplate內(nèi)建函數(shù)assign()來(lái)賦值的
具體語(yǔ)法如下
code: assign ( 模版中的變量, 要替換的內(nèi)容 ) |
或
code: assign ( Array內(nèi)容 ) |
正如其他程序的變量一樣,smartTemplate的變量是由特殊的{}所包含的。里面的內(nèi)容可以是String,Array,Int,或者是Long Text等等(基本上php支持的)
在儲(chǔ)存Array數(shù)據(jù)時(shí),smartTemplate運(yùn)用了我們常見(jiàn)的父子級(jí)分割符".",所以一個(gè)特殊的Array數(shù)據(jù)由Array Handle和具體位置的索引組成(Numerical Index or Associative Index)。
下面是一個(gè)例子
在php環(huán)境下運(yùn)行以下程序
code: <?php $template = new SmartTemplate('template.html'); $text = 'Sample Text'; $template->assign( 'TITLE', $text ); $template->output(); ?> |
模版
code: <html> {TITLE} </html> |
輸出
code: <html> Sample Text </html> |
在只有一個(gè)Array的情況下,可以直接省略前面的array handle,就象在使用javascript時(shí),document.window.close()可以省略為window.close()
code: <?php $user = array( 'NAME' => 'John Doe', 'GROUP' => 'Admin', 'AGE' => '42', ); $template = new SmartTemplate('user.html'); $template->assign( $user ); $template->output(); ?> |
模版
code: Name: {NAME} Group: {GROUP} Age: {AGE} 輸出 CODE Name: John Doe Group: Admin Age: 42 |
下面是另外一個(gè)例子。使用了SmartTemplate的循環(huán)函數(shù)<!-- begin Array名 -->XXXXXX<!-- end Array名>
他的功能類似foreach(),只要有東西,就一直循環(huán)顯示
code: <?php $links = array( array( 'TITLE' => 'PHP', 'URL' => 'http://www.php.net/', ), array( 'TITLE' => 'Apache', 'URL' => 'http://www.php.net/', ), array( 'TITLE' => 'MySQL', 'URL' => 'http://www.mysql.com/', ), ); $template = new SmartTemplate('links.html'); $template->assign( 'links', $links ); $template->output(); ?> |
HTML模版
code: <html> <h3> Sample Links </h3> <!-- BEGIN links --> <a href="../{URL}"> {TITLE} </a> <!-- END links --> </html> CODE <html> <h3> Sample Links </h3> <a href="../ <a href="../ <a href="../</html> |
3、SmartTemplate的邏輯控制結(jié)構(gòu)
★If和end If
語(yǔ)法:
<!-- IF 變量 --> 變量已被賦值! <!-- ENDIF 變量 -->
如果IF后面直接跟變量,變量為Null時(shí)會(huì)返回0,否則返回1
<!-- IF name=="John Doe" --> Your name is John Doe! <!-- ENDIF name -->
==判斷是否相等,如果相等返回1,不相等返回0
<!-- IF name!="John Doe" --> Your name is not John Doe! <!-- ENDIF name -->
!=判斷是否不等,如果成立返回1,相等則返回0
例子:
code: <?php require_once "class.smarttemplate.php"; $page = new SmartTemplate("if.html"); $page->assign( 'username', 'John Doe' ); $page->assign( 'usergroup', 'ADMIN' ); $page->assign( 'picture', '' ); $page->output(); ?> |
HTML
code: <!-- IF username --> <H3> Welcome, {username} </H3> <!-- ENDIF --> <!-- IF picture --> <img src="{picture}"> <!-- ENDIF picture --> <!-- IF usergroup="ADMIN" --> <a href="../admin.php"> ADMIN Login </a><br> <!-- ENDIF usergroup --> |
輸出
code: <H3> Welcome, John Doe </H3> <a href="../admin.php"> ADMIN Login </a><br> |
★IF的子局 else
如果else子句出現(xiàn)在一個(gè)邏輯循環(huán)中,當(dāng)if的條件不成立時(shí)則會(huì)被運(yùn)行。
例子
code: <?php require_once "class.smarttemplate.php"; $page = new SmartTemplate("else.html"); $page->assign( 'username', 'John Doe' ); $page->assign( 'usergroup', 'ADMIN' ); $page->assign( 'picture', '' ); $page->output(); ?> |
模版
code: <!-- IF username --> <H3> Welcome, {username} </H3> <!-- ENDIF --> <!-- IF picture --> <img src="{picture}"> <!-- ELSE --> Picture not available! <br> <!-- ENDIF picture --> <!-- IF usergroup="ADMIN" --> <a href="../admin.php"> ADMIN Login </a><br> <!-- ELSE --> You are in guest mode! <!-- ENDIF usergroup --> |
輸出
code: <H3> Welcome, John Doe </H3> Picture not available! <br> <a href="../admin.php"> ADMIN Login </a><br> |
★elseif
elseif是else和if組合起來(lái)的一種結(jié)構(gòu),其意義為"除此之外如果..."
以下是一個(gè)例子
code: <?php require_once "class.smarttemplate.php"; $page = new SmartTemplate("elseif.html"); $page->assign( 'usergroup', 'INTERNAL' ); $page->output(); ?> |
模版文件
code: <!-- IF usergroup="ADMIN" --> <a href="../admin.php"> 管理員登陸 </a><br> <!-- ELSEIF usergroup="SUPPORT" --> <a href="../support.php"> 幫助人員登陸</a><br> <!-- ELSEIF usergroup --> <a href="../other.php"> 普通方式登陸 </a><br> <!-- ELSE --> You don't even have a usergroup! <!-- ENDIF --> |
運(yùn)行php得到的輸出
code: <a href="../other.php"> 普通方式登陸 </a><br> |
★Begin...End
這 個(gè)語(yǔ)句用于讀取一個(gè)整數(shù)索引矩陣(Numerical Array,以數(shù)字為索引的數(shù)組)的值.而每個(gè)整數(shù)矩陣的子矩陣則成為以字符串為索引的矩陣(Associative Array)然后在<!-- begin --> 和 <!-- end -->之間的語(yǔ)句將會(huì)被讀取并且重復(fù)執(zhí)行.
附加:,每個(gè)associative array(字符串為索引的矩陣)會(huì)有兩個(gè)附加的值
ROWCNT : 此元素在父矩陣中的具體位置 (0,1,2,3,...n) (就是目前在第幾個(gè)矩陣)
ROWBIT : 矩陣序號(hào)的最后一位. (0,1,0,1,0,1,...)
下面是一個(gè)例子
PHP代碼:
code: <?php require_once "class.smarttemplate.php"; $page = new SmartTemplate("begin_end.html"); $users = array( array( 'NAME' => 'John Doe', 'GROUP' => 'ADMIN' ), array( 'NAME' => 'Jack Doe', 'GROUP' => 'SUPPORT' ), array( 'NAME' => 'James Doe', 'GROUP' => 'GUEST' ), array( 'NAME' => 'Jane Doe', 'GROUP' => 'GUEST' ), ); $page->assign( 'users', $users ); $page->output(); ?> |
HTML模版
code: <style type="text/css"> .col0 { background-color: #D0D0D0; } .col1 { background-color: #F0F0F0; } </style> <table border="1" cellpadding="2" cellspacing="0"> <tr> <th> No </th> <th> Username </th> <th> Usergroup </th> </tr> <!-- BEGIN users --> <tr class="col{ROWBIT}"> <td> {ROWCNT} </td> <td> {NAME} </td> <td> {GROUP} </td> </tr> <!-- END users --> </table> |
最后的輸出
code: <style type="text/css"> .col0 { background-color: #D0D0D0; } .col1 { background-color: #F0F0F0; } </style> <table border="1" cellpadding="2" cellspacing="0"> <tr> <th> No </th> <th> Username </th> <th> Usergroup </th> </tr> <tr class="col0"> <td> 0 </td> <td> John Doe </td> <td> ADMIN </td> </tr> <tr class="col1"> <td> 1 </td> <td> Jack Doe </td> <td> SUPPORT </td> </tr> <tr class="col0"> <td> 2 </td> <td> James Doe </td> <td> GUEST </td> </tr> <tr class="col1"> <td> 3 </td> <td> Jane Doe </td> <td> GUEST </td> </tr> </table> |
☆smartTemplate的方法
注:以下列出的方法并不會(huì)在模版設(shè)計(jì)中出前,他們屬于smartTemplate的代碼編輯部分,但是如果為了實(shí)現(xiàn)更深一步的模版設(shè)計(jì),下面的內(nèi)容是肯定有用的.
★基礎(chǔ)方法:assign (中文意思:賦值)
語(yǔ)法:
assign ( 變量名, 混合內(nèi)容 )
或者
assign ( 矩陣變量 )
更多介紹在變量介紹部分
★Append(附加)
語(yǔ)法:append ( 變量名, 內(nèi)容 )
對(duì)所提供的變量附加提供的內(nèi)容
例子:
code: <?php $page = new SmartTemplate('links.html'); $page->append('links' => array( 'TITLE' => 'PHP', 'URL' => 'http://www.php.net/' )); $page->append('links' => array( 'TITLE' => 'Apache', 'URL' => 'http://www.apache.org/' )); $page->append('links' => array( 'TITLE' => 'MySQL', 'URL' => 'http://www.mysql.com/' )); $page->output(); ?> |
模版
code: <html> <h3> Sample Links </h3> <!-- BEGIN links --> <a href="../{URL}"> {TITLE} </a> <!-- END links --> </html> |
最終輸出
code: <html> <h3> Sample Links </h3> <a href="../http://www.php.net/"> PHP </a> <a href="../http://www.apache.org/"> Apache </a> <a href="../http://www.mysql.com/"> MySQL </a> </html> |
另外一個(gè)附加字符串的例子:
code: <?php $page = new SmartTemplate('template.html'); $page->append('TITLE' => 'Hello '); $page->append('TITLE' => 'World '); $page->append('TITLE' => '!'); $page->output(); ?> |
輸出將會(huì)得到
code:
<html> Hello World ! </html>