在Smarty模板函数里面有这样一个方法:fetch("template.htm"),他和display("template.htm");最大的不同就是fetch()是把内容输出给一个变量,而display()是把内容输出给浏览器,这样我们就可以用一个变量来接收fetch()的输出,然后把他写入到文件中去.
require_once(DIRROOT.'smarty/Smarty.class.php');
$smarty = new Smarty();
$smarty->template_dir = DIRROOT.'/smarty/template/';
$smarty->compile_dir = DIRROOT.'/smarty/tempcomp/';
$smarty->cache_dir = DIRROOT.'/smarty/caches/';
$smarty->cache_lifetime = 600 ;
$smarty->left_delimiter = "{-";
$smarty->right_delimiter = "-}";
$smarty->caching = false;
//$smarty->caching = true;
$html=$smarty->fetch("template.htm");
$fp = fopen($file_name,"w+");
if(!fwrite($fp,$html)){
die('生成html文件失败!');
}
fclose($fp);