首页 > php框架 > smarty生成批量生成html

smarty生成批量生成html

再来讲一个简单的用smarty生成批量生成html文件的实例教程吧,下面我们来看看讲得比较清楚哦.

模板文件new.tpl

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title><{$news.titles}></title>
<link href="<{$docroot}>/CSS/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0" style="border:solid #CCCCCC 1px">
  <tr>
    <td height="25" colspan="6" align="center" bgcolor="#eeeeee"><strong><{$news.titles}></strong></td>
  </tr>
  <tr>
    <td width="74" height="25" align="right">作者:</td>
    <td width="220"><{$news.author}></td>
    <td width="40" align="right">时间:</td>
    <td width="134" align="left"><{$news.sj}></td>
    <td width="110" align="right">关键词:</td>
    <td width="220" align="left"><{$news.keyword}></td>
  </tr>
  <tr>
    <td height="25" align="right" >核心提示:</td>
    <td height="25" colspan="5" class="artical" style="border:double #FF0000 3px;"><{$news.sumary}></td>
  </tr>
  <tr>
    <td height="25" colspan="6" align="left" class="artical" style="padding:10px;"><{$news.contents}></td>
  </tr>
</table>
</body>
</html>

生成html文件函数写在smarty里面.

function MakeHtmlFile($file_name, $c) 
{  
      if(!$fp = fopen($file_name, "wa"))
      {     
         echo "文件打开失败!";     
         return false;     
      }     
      if(!fwrite($fp, $c))
      {     
         echo "文件写入失败!";     
         fclose($fp);     
         return false;     
      }             
      fclose($fp);     
   }

n.php读取内容发送给smarty

<?php
include_once ("config.php");
include_once ("init.php");
$s->assign("title", "所有的新闻分类");
$ID = $_GET["ID"] + 0;
$sql = "select * from artical where newsID=$ID";
$rs = $db->fetch($sql);
$s->assign("news", $rs["rec"][0]); //注意:$rs["rec"][0]是个数组
$s->display("news.html");
?>
生成文件使用makeHtmlFile
<?php
include_once ("config.php");
include_once ("init.php");
$sql = "select * from artical";
$rs = $db->fetch($sql);
foreach ($rs["rec"] as $k => $v) {
    $s->assign("news", $v);
    $s->MakeHtmlFile("./news/news_" . $v[0] . ".html", $s->fetch("news.html", null, null, false));
}
?>


本文地址:http://www.phprm.com/frame/dbe577743bbb13d91559d01bd75d9010.html

转载随意,但请附上文章地址:-)

标签:none

发表留言