PHP之Smarty简单实现
生活随笔
收集整理的這篇文章主要介紹了
PHP之Smarty简单实现
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
首先,加載 Smarty 模版引擎,建立 Smarty 對象,設定 Smarty 對象的參數。
//調用?Smarty
$template_name?=?'test';????????????????//模版文件夾名
require_once(?ROOT_PATH?.?'includes/smarty/Smarty.class.php');????????//包含smarty類文件
$smarty=new?Smarty;????????????????????????//初始化Smarty
$smarty->compile_check?=?true;????????????//打開模版編譯檢查
$smarty->debugging?=?false;????????????????//關閉調試
$smarty->caching?=?false;????????????????//關閉緩存
$smarty->template_dir?=?"template/{$template_name}/";????//設置模版路徑
$smarty->compile_dir?=?'template_c';????//設置編譯文件存放的文件夾
$smarty->left_delimiter?=?'[##';????????//設置左邊界符
$smarty->right_delimiter?=?'##]';????????//設置右邊界符號 然后,調用函數:
require_once(?ROOT_PATH?.?'mods/mod_main.php');
$func?=?'main_page';
$main_body?=?$func();????//調用函數,實現不同功能,并用$main_body接收返回結果(其實就是一堆HTML) main_page函數如下:
function?main_page()????????//默認調用的函數
{
????global?$smarty;
????
????require_once(?ROOT_PATH?.?'includes/ubb.inc.php');????????//包含UBB文件
????$message_array?=?array();
????for($i=0;$i<10;$i++)????????????????//循環取出結果
????{
????????//把結果存成二維數組
????????$message_array[]?=?array(
????????????'id'?????????=>?$i,
????????????'username'?????=>?'測試username',
????????????'content'????=>?ubb('測試content'),????????????//用ubb函數過濾變量
????????);
????}
????$smarty->assign('message'?,?$message_array);????????????//替換模板變量
????return?$smarty->fetch('message.tpl');
} 利用 Smarty 的 display 方法將網頁秀出:
$smarty->assign('main_body'?,?$func());????????//smarty方法,用來把模板里的[##$main_body##]部分用變量$main_body替換。
$smarty->display('test.tpl');????????????//輸出模板
源代碼:Smarty簡單示例
Smarty中文文檔:Smarty中文文檔
//調用?Smarty
$template_name?=?'test';????????????????//模版文件夾名
require_once(?ROOT_PATH?.?'includes/smarty/Smarty.class.php');????????//包含smarty類文件
$smarty=new?Smarty;????????????????????????//初始化Smarty
$smarty->compile_check?=?true;????????????//打開模版編譯檢查
$smarty->debugging?=?false;????????????????//關閉調試
$smarty->caching?=?false;????????????????//關閉緩存
$smarty->template_dir?=?"template/{$template_name}/";????//設置模版路徑
$smarty->compile_dir?=?'template_c';????//設置編譯文件存放的文件夾
$smarty->left_delimiter?=?'[##';????????//設置左邊界符
$smarty->right_delimiter?=?'##]';????????//設置右邊界符號 然后,調用函數:
require_once(?ROOT_PATH?.?'mods/mod_main.php');
$func?=?'main_page';
$main_body?=?$func();????//調用函數,實現不同功能,并用$main_body接收返回結果(其實就是一堆HTML) main_page函數如下:
function?main_page()????????//默認調用的函數
{
????global?$smarty;
????
????require_once(?ROOT_PATH?.?'includes/ubb.inc.php');????????//包含UBB文件
????$message_array?=?array();
????for($i=0;$i<10;$i++)????????????????//循環取出結果
????{
????????//把結果存成二維數組
????????$message_array[]?=?array(
????????????'id'?????????=>?$i,
????????????'username'?????=>?'測試username',
????????????'content'????=>?ubb('測試content'),????????????//用ubb函數過濾變量
????????);
????}
????$smarty->assign('message'?,?$message_array);????????????//替換模板變量
????return?$smarty->fetch('message.tpl');
} 利用 Smarty 的 display 方法將網頁秀出:
$smarty->assign('main_body'?,?$func());????????//smarty方法,用來把模板里的[##$main_body##]部分用變量$main_body替換。
$smarty->display('test.tpl');????????????//輸出模板
源代碼:Smarty簡單示例
Smarty中文文檔:Smarty中文文檔
轉載于:https://www.cnblogs.com/zhuboxingzbx/articles/1235437.html
總結
以上是生活随笔為你收集整理的PHP之Smarty简单实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL 语句中的字段名 如果是 关键字
- 下一篇: .net中的socket异步通信实现--