首先,加载 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中文文档 相关文章: