用Smarty模板生成html文件,其实就是在 display() 的同时,增加了 ob_start()、b_get_contents() 和 fwrite() 函数。
具体实现方法,请看下面两个文件:
文件一: generate_html.php
require_once('inc/smarty.inc.php');
require_once('include/config.php');
ob_start();
$title = "title";
$description = "description";
$keywords = "keywords";
$outfilename = "test.html";
$smarty->assign("TITLE", $title);
$smarty->assign("DESCRIPTION", $description);
$smarty->assign("KEYWORDS", $keywords);
$smarty->assign("CSSPATH", "/css");
$smarty->assign("TPL_LEFT", TPL_LEFT);
$smarty->assign("TPL_RIGHT", TPL_RIGHT);
$smarty->assign("TPL_TOP", TPL_TOP);
$smarty->assign("TPL_FOOTER", TPL_FOOTER);
$smarty->assign("TPL_CENTER", TPL_CATEGORY);
$smarty->display(TPL_MAIN); // TPL_MAIN 等常量在 include/config.php 中已经被定义
$str = ob_get_contents();
$fp = @fopen($outfilename, 'w');
if (!$fp) {
Show_Error_Message( ERROR_WRITE_FILE );
}
fwrite($fp, $str);
fclose($fp);
ob_end_clean();
?>
文件二: templates/main.htm
<{config_load file="global.conf"}>
<{$TITLE}>
<{include file="$TPL_TOP"}> |
<{include file="$TPL_LEFT"}> |
<{include file="$TPL_CENTER"}> |
<{include file="$TPL_RIGHT"}> |
<{include file="$TPL_FOOTER"}> |
阅读(904) | 评论(0) | 转发(0) |