新进化论

道生一,一生二,二生三,三生万物。

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
Smarty是PHP的模板引擎之一。简单的讲就是
在你的php程序里你把要用到的变量分配到模板中,然后将他们用于页面输出.

       Smarty和PHPA(或Zend Cache)的不同:PHPA只是在内存中缓存了PHP脚本编译过的bytecode。这加速了服务器的响应时间和节省了编译步骤,而Smarty创建的PHP script,PHPA也能很好的做Cache,现在Smarty内置的cache则完全不同,它cache了模板内容的输出,例如:如果你有一个需要几个数据库查询的模板,Smarty可以cache输出,省去了每次调用数据库的必要.Smarty能和PHPA或Zend Cache很好的一同工作。
Smarty只被PHP 4.06以上版本支持。
      解開 Smarty 2.6.x 後,會看到很多檔案,其中有個 libs 資料夾。在 libs 中應該會有 3 個 class.php 檔 + 1 個 debug.tpl + 1 個 plugin 資料夾 + 1 個 core 資料夾。接著在您的程式主資料夾中建立一個叫 class 的資料夾,然後將 libs 底下的這些檔案及資料夾複製到 class 這個資料夾裡面就完成安裝啦!或是直接將 libs 複製到您的程式主資料夾下,再更名為 class 也可以。這種安裝法比較簡單,一般沒有自己主機的使用者可以參考看看;當然如果您已經對 PHP 很熟的話,可以將 Smarty 安裝到一般 User 無法到訪的資料夾中,例如/usr/local/lib/php/smarty/Smarty.class.php,然后requrie 该文件就可以了。

例1:index.php
include('Smarty.class.php');

// create object[创建对象]
$smarty = new Smarty;

// assign some content[分配一些内容]
$smarty->assign('name', 'george smith');
$smarty->assign('address', '45th & Harris');

// display it[输出模板]
$smarty->display('index.tpl');




模板文件[tpl]包含了所要输出的内容的标签,Smarty将用你所分配的内容替换他们
index.tpl

















header.tplfooter.tpl
<html>
<head>
<title>{$title|default:"no title"}</title>
</head>
<body>
 
</body>
</html>
index.tploutput
{include file="header.tpl" title="User Info"}

User Information:<p>

Name: {$name|capitalize}<br>
Address: {$address|escape}<br>

{include file="footer.tpl"}
 
<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: George Smith<br>
Address: 45th &amp; Harris<br>

</body>
</html>






output
<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: {$name}<br>
Address: {$address}<br>

</body>
</html>
 
<html>
<head>
<title>User Info</title>
</head>
<body>

User Information:<p>

Name: george smith<br>
Address: 45th & Harris<br>

</body>
</html>

Smarty有这样的一个特点(称之为变量调节器):可以在模板内转换所要分配的变量.

例如,我们希望让Geoge的名字首字母大写,而在地址里希望html输出&amp;字符

干得漂亮!(ft...)你同样可以将调节器链接到一个变量上面,让它更灵活.

Smarty自带了很多调节器,或者你可以使用插件机制diy一个.

把你自己做的调节器放到插件目录里,就会在模板里起作用!

Smarty同样可以用模板函数来完成任务.

例如,你可以在模板里使用include函数来包含其他模板.

比如说你有很多模板都带有相同的头,尾信息,你可以为它们独立制作成模板,再include它们.

注意到$title是一个没有在php代码里被分配的模板变量,但是却被include函数传递了过来.

这种方法使得$title变量只在Header模板里有作用.在任何时候header.tpl被include的时候都可被动态替换.

同样,注意到default调节器应用到了$title,在没有任何标题的情况下,将显示"no title"

smarty同样也有很多很好的自动完成函数,比如html列表.

其中一个是 html_options.你可以给模板分配一个数组的数据,这个函数就完成要求一系列的输出工作.











index.php
include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign options arrays
$smarty->assign('id', array(1,2,3,4,5));
$smarty->assign('name', array('bob','jim','joe','jerry','fred'));

// display it
$smarty->display('index.tpl');










index.tpl
<select name=user>
{html_options values=$id output=$name selected="5"}
</select>










output
<select name=user>
<option label="bob" value="1">bob</option>
<option label="jim" value="2">jim</option>
<option label="joe" value="3">joe</option>
<option label="jerry" value="4">jerry</option>
<option label="fred" value="5" selected>fred</option>

</select>

Smarty可以使用section函数轻松的遍历数组.

这里有个例子,我们同时也用了cycle函数插入行交替颜色,使用strip函数换新的一行并留出空白
include('Smarty.class.php');

// create object
$smarty = new Smarty;

// assign an array of data
$smarty->assign('name', array('bob','jim','joe','jerry','fred'));

// assign an associative array of data
$smarty->assign('users', array(
array('name' => 'bob', 'phone' => '555-3425'),
array('name' => 'jim', 'phone' => '555-4364'),
array('name' => 'joe', 'phone' => '555-3422'),
array('name' => 'jerry', 'phone' => '555-4973'),
array('name' => 'fred', 'phone' => '555-3235')
));



// display it
$smarty->display('index.tpl');






index.tpl
<table>
{section name=mysec loop=$name}
{strip}
<tr bgcolor="{cycle values="#eeeee,#dddddd"}">
<td>{$name[mysec]}</td>
</tr>
{/strip}
{/section}
</table>

<table>
{section name=mysec loop=$users}
{strip}
<tr bgcolor="{cycle values="#aaaaaa,#bbbbbb"}">
<td>{$users[mysec].name}</td>
<td>{$users[mysec].phone}</td>
</tr>
{/strip}
{/section}
</table>










output
<table>
<tr bgcolor="#eeeeee"><td>bob</td></tr>
<tr bgcolor="#dddddd"><td>jim</td></tr>
<tr bgcolor="#eeeeee"><td>joe</td></tr>
<tr bgcolor="#dddddd"><td>jerry</td></tr>
<tr bgcolor="#eeeeee"><td>fred</td></tr>
</table>

<table>
<tr bgcolor="#aaaaaa"><td>bob</td><td>555-3425</td></tr>
<tr bgcolor="#bbbbbb"><td>jim</td><td>555-4364</td></tr>
<tr bgcolor="#aaaaaa"><td>joe</td><td>555-3422</td></tr>
<tr bgcolor="#bbbbbb"><td>jerry</td><td>555-4973</td></tr>
<tr bgcolor="#aaaaaa"><td>fred</td><td>555-3235</td></tr>
</table>







bob
jim
joe
jerry
fred







bob 555-3425
jim 555-4364
joe 555-3422
jerry 555-4973
fred 555-3235

Smarty有很多内建和自定义函数,你也可以使用插件机制自己做.


请注意smarty的所有专用于内容展示的功能(调节器,函数,等等).

这是分隔你的表示层的很重要的方面.

所有的应用/商业逻辑都应该保留在你的应用程序(后台)里 ,然而所有的有关页面输出,展示的冬冬都应该在模板里处理.

以上是smarty应用的范例.


posted on 2004-08-26 09:27  岌岌可危  阅读(844)  评论(0)    收藏  举报