让Smarty模板生成html文件
用Smarty模板生成html文件,其实就是在 display() 的同时,增加了 ob_start()、b_get_contents() 和 fwrite() 函数。
具体实现方法,请看下面两个文件:
文件一: generate_html.php
<?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
<html>
<head>
<{config_load file=双引号global.conf双引号}>
<title><{$TITLE}></title>
<meta name=双引号description双引号 content=双引号<{$DESCRIPTION}>双引号>
<meta name=双引号keywords双引号 content=双引号<{$KEYWORDS}>双引号>
<meta http-equiv=双引号Content-Type双引号 content=双引号text/html; charset=iso-8859-1双引号>
<link rel=双引号stylesheet双引号 href=双引号<{$CSSPATH}>/style.css双引号 type=双引号text/css双引号>
</HEAD>
<body>
<table width=双引号<{#TABLE_WIDTH#}>双引号 border=双引号0双引号 cellspacing=双引号0双引号 cellpadding=双引号0双引号 align=双引号center双引号>
<tr>
<td colspan=双引号3双引号>
<{include file=双引号$TPL_TOP双引号}>
</td>
</tr>
<tr>
<td width=双引号<{#TABLE_LEFT_WIDTH#}>双引号 align=双引号left双引号 valign=双引号top双引号>
<{include file=双引号$TPL_LEFT双引号}>
</td>
<td width=双引号<{#TABLE_CENTER_WIDTH#}>双引号 align=双引号center双引号 valign=双引号top双引号>
<{include file=双引号$TPL_CENTER双引号}>
</td>
<td width=双引号<{#TABLE_RIGHT_WIDTH#}>双引号 align=双引号right双引号 valign=双引号top双引号>
<{include file=双引号$TPL_RIGHT双引号}>
</td>
</tr>
<tr>
<td colspan=双引号3双引号>
<{include file=双引号$TPL_FOOTER双引号}>
</td>
</tr>
</table>
</body>
</html>文章来源:http://it503.com/detail.PHP
关键词:让Smarty模板生成html文件,php
具体实现方法,请看下面两个文件:
文件一: generate_html.php
<?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
<html>
<head>
<{config_load file=双引号global.conf双引号}>
<title><{$TITLE}></title>
<meta name=双引号description双引号 content=双引号<{$DESCRIPTION}>双引号>
<meta name=双引号keywords双引号 content=双引号<{$KEYWORDS}>双引号>
<meta http-equiv=双引号Content-Type双引号 content=双引号text/html; charset=iso-8859-1双引号>
<link rel=双引号stylesheet双引号 href=双引号<{$CSSPATH}>/style.css双引号 type=双引号text/css双引号>
</HEAD>
<body>
<table width=双引号<{#TABLE_WIDTH#}>双引号 border=双引号0双引号 cellspacing=双引号0双引号 cellpadding=双引号0双引号 align=双引号center双引号>
<tr>
<td colspan=双引号3双引号>
<{include file=双引号$TPL_TOP双引号}>
</td>
</tr>
<tr>
<td width=双引号<{#TABLE_LEFT_WIDTH#}>双引号 align=双引号left双引号 valign=双引号top双引号>
<{include file=双引号$TPL_LEFT双引号}>
</td>
<td width=双引号<{#TABLE_CENTER_WIDTH#}>双引号 align=双引号center双引号 valign=双引号top双引号>
<{include file=双引号$TPL_CENTER双引号}>
</td>
<td width=双引号<{#TABLE_RIGHT_WIDTH#}>双引号 align=双引号right双引号 valign=双引号top双引号>
<{include file=双引号$TPL_RIGHT双引号}>
</td>
</tr>
<tr>
<td colspan=双引号3双引号>
<{include file=双引号$TPL_FOOTER双引号}>
</td>
</tr>
</table>
</body>
</html>文章来源:http://it503.com/detail.PHP
添加收藏到:
关键词:让Smarty模板生成html文件,php
