目錄
1、SmartTemplate的效率
2、基本變量
3、邏輯運算結(jié)構(gòu)
4、模式(Methods)
5、擴展類(Extensions) 未完成
------------------------------------------------------------------------
1、SmartTemplate的效率
雖然他有很多的程序來形成強大的功能,但在執(zhí)行時只有你調(diào)用的才被導(dǎo)入,所以不用擔心這方面的速度影響,同樣這套模版系統(tǒng)是為最快的執(zhí)行效率而優(yōu)化過的,比起目前市場上常見的Smarty,要快不少(Smarty采用后期緩存,所以比較可能不是很準確)。
2、SmartTemplate的變量
Array的變量是由SmartTemplate內(nèi)建函數(shù)assign()來賦值的
具體語法如下
assign ( 模版中的變量, 要替換的內(nèi)容 )
或
assign ( Array內(nèi)容 )
正如其他程序的變量一樣,smartTemplate的變量是由特殊的{}所包含的。里面的內(nèi)容可以是String,Array,Int,或者是Long Text等等(基本上php支持的)
在儲存Array數(shù)據(jù)時,smartTemplate運用了我們常見的父子級分割符".",所以一個特殊的Array數(shù)據(jù)由Array Handle和具體位置的索引組成(Numerical Index or Associative Index)。
下面是一個例子
在php環(huán)境下運行以下程序
代碼:
<?php
$template = new SmartTemplate('template.html');
$text = 'Sample Text';
$template->assign( 'TITLE', $text );
$template->output();
?>
模版
代碼:
<html> {TITLE} </html>
輸出
代碼:
<html> Sample Text </html>
在只有一個Array的情況下,可以直接省略前面的array handle,就象在使用javascript時,document.window.close()可以省略為window.close()
php
代碼:
<?php
$user = array(
'NAME' => 'John Doe',
'GROUP' => 'Admin',
'AGE' => '42',
);
$template = new SmartTemplate('user.html');
$template->assign( $user );
$template->output();
?>
模版
代碼:
Name: {NAME}
Group: {GROUP}
Age: {AGE}
輸出
代碼:
Name: John Doe
Group: Admin
Age: 42
下面是另外一個例子。使用了SmartTemplate的循環(huán)函數(shù)<!-- begin Array名 -->XXXXXX<!-- end Array名>
他的功能類似foreach(),只要有東西,就一直循環(huán)顯示
代碼:
<?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模版
代碼:
<html>
<h3> Sample Links </h3>
<!-- BEGIN links -->
<a href="{URL}"> {TITLE} </a>
<!-- END links -->
</html>
代碼:
<html>
<h3> Sample Links </h3>
<a > PHP </a>
<a > Apache </a>
<a > MySQL </a>
</html>
新聞熱點
疑難解答
圖片精選