擴展設置
這是基本安裝的繼續,請先閱讀那個檔!
一個更靈活一點的配置Smarty的方法是擴展類,和初始化你的smarty環境。
為了避免重複地配置路徑,我們可以在一個檔裏配置這些變數。
我們創建一個目錄 "/php/includes/guestbook/" 建立一個檔"setup.php"
同樣先設置好smarty路徑。
例2-10.編輯 /php/includes/guestbook/setup.php
// load Smarty library
require('Smarty.class.php');
// The setup.php file is a good place to load
// required application library files, and you
// can do that right here. An example:
// require('guestbook/guestbook.lib.php');是一個很好的加載應用程式的類庫檔(就是擴展類)
//例如你可以在index檔裏包含它
class Smarty_GuestBook extends Smarty {
function Smarty_GuestBook() {
// Class Constructor. These automatically get set with each new instance.
//類構造函數.創建實例的時候自動配置
$this->Smarty();
$this->template_dir = '/web/www.mydomain.com/smarty/guestbook/templates/';
$this->compile_dir = '/web/www.mydomain.com/smarty/guestbook/templates_c/';
$this->config_dir = '/web/www.mydomain.com/smarty/guestbook/configs/';
$this->cache_dir = '/web/www.mydomain.com/smarty/guestbook/cache/';
$this->caching = true;
$this->assign('app_name','Guest Book');
}
}
|
|
現在我們針對setup檔更改一下index檔
Smarty手冊範例 2-11.編輯/web/www.mydomain.com/docs/guestbook/index.php
require('guestbook/setup.php');
$smarty = new Smarty_GuestBook;
$smarty->assign('name','Ned');
$smarty->display('index.tpl');
|
|
現在你看到創建一個使用smarty的實例有多麼的簡單.從Smarty_GuestBook開始,重新構建我們的應用程式吧^_^