smarty的最新網址:http://www.smarty.net/
到這個頁面http://www.smarty.net/download.php
下載最新的smarty源檔案。
解壓下載的檔案,把libs檔案夾及目錄下的所有檔案拷貝到的一新的檔案夾如:smarty_temp目錄下面,然後
再在這個目錄下面建立立四個檔案夾:templates,templates_c,cache,configs
然後再建立立一個設定檔:config.inc.php,內容如下:
<?php
/*
* 只要把此檔案放在和templates,templates_c,cache,configs同一個目錄下,
* 在其它地方只要調用這個檔案,就可以顯示出來了。
*
*/
// 把D:/phpweb/myweb/smarty_temp路徑換成D:/phpweb/myweb/smarty_temp
$PathArray = explode('smarty_temp',str_replace('//','/',dirname(__FILE__)));
define('ROOT',$PathArray[0]); //設定網站檔案的絕對路徑
define('SMARTY_ROOT',ROOT.'smarty_temp/'); //設定Smarty類庫路徑
require_once 'libs/Smarty.class.php';
$Smarty = new Smarty(); //建立新的Smarty對象
$Smarty -> compile_check = TRUE; //每次PHP執行時查看模板檔案的內容是否改變。
//debugging控制台,這是一個切換參數,如果開啟它將顯示Smarty變數和運行狀態的調試視窗。
$Smarty -> debugging = FALSE;
//設定模板目錄
$Smarty -> template_dir = SMARTY_ROOT.'templates/';
//Smarty預設的模板目錄名:templates_c
$Smarty -> compile_dir = SMARTY_ROOT.'templates_c/';
//預設範本緩衝的目錄名
$Smarty -> cache_dir = SMARTY_ROOT.'cache/';
//預設的config檔案目錄名:configs
$Smarty -> config_dir = SMARTY_ROOT.'configs/';
//左右邊界符,預設為{},但實際應用當中容易與JavaScript相衝突,所以建議設成<{}>或其它。
$Smarty -> left_delimiter = '<{';
$Smarty -> right_delimiter = '}>';
?>
測試一下:
在tmeplates目錄下建立test.html模板檔案,內容如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title><{$title}></title>
</head>
<body>
<{$content}>
</body>
</html>
然後在smarty_temp目錄下建立test.php檔案,內容如下:
<?php
require_once 'Config.inc.php';//只要能把這個檔案包含進來,test.php檔案放在哪都可以。
$Smarty->assign("title", "測試成功了,這是標題"); //進行模板變數替換
$Smarty->assign("content", "測試結果成功"); //進行模板變數替換
$Smarty->display('test.htm');//編譯並顯示位於./templates下的index.tpl模板
?>
運行一下就可以了。