Smarty中批量生成html的方法

 

首先在smartt.class.php中为Smarty类添加这么一个方法:
//参数一:html文件保存路径,参数2:写入的内容


问题:我的所有文章都调用的是news.tpl这个模板那么究竟如何批量生成呢?

  1. function MakeHtmlFile($file_name$c
  2. {   
  3.       if(!$fp = fopen($file_name"wa")) 
  4.       {     
  5.          echo "文件打开失败!";     
  6.          return false;     
  7.       }     
  8.       if(!fwrite($fp$c)) 
  9.       {     
  10.          echo "文件写入失败!";     
  11.          fclose($fp);     
  12.          return false;     
  13.       }             
  14.       fclose($fp);     
  15.    } 

我们先看看news.php PHP代码:

 

    1. <?php  
    2. include_once("config.php"); 
    3. include_once("init.php"); 
    4. $s->assign("title","所有的新闻分类"); 
    5. $ID=$_GET["ID"]+0; 
    6. $sql="select * from artical where newsID=$ID"
    7. $rs=$db->fetch($sql); 
    8. $s->assign("news",$rs["rec"][0]);//注意:$rs["rec"][0]是个数组  
    9. $s->display("news.html"); 
    10. ?> 
    整个模板变量就只有$news,这么写有什么好处呢?我可以以数组的方式读取内容

    那么究竟如何生成:
    看如下代码:
    很简单的 PHP代码:



    1. <?php 
    2. include_once("config.php"); 
    3. include_once("init.php"); 
    4. $sql="select * from artical"
    5. $rs=$db->fetch($sql); 
    6. foreach ($rs["rec"as $k=>$v
    7. $s->assign("news",$v); 
    8. $s->MakeHtmlFile("./news/news_".$v[0].".html",$s->fetch("news.html",null, null, false)); 
    9. ?>

    很简单把!

    PHP代码:

    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
    2. <html xmlns="http://www.w3.org/1999/xhtml"
    3. <head> 
    4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
    5. <title><{$news.titles}></title> 
    6. <link href="<{$docroot}>/CSS /main.css" rel="stylesheet" type="text/css" /> 
    7. </head> 
    8. <body> 
    9. <table width="800" border="0" align="center" cellpadding="0" cellspacing="0" style="border:solid #CCCCCC 1px"
    10.   <tr> 
    11.     <td height="25" colspan="6" align="center" bgcolor="#eeeeee"><strong><{$news.titles}></strong></td> 
    12.   </tr> 
    13.   <tr> 
    14.     <td width="74" height="25" align="right">作者:</td> 
    15.     <td width="220"><{$news.author}></td> 
    16.     <td width="40" align="right">时间:</td> 
    17.     <td width="134" align="left"><{$news.sj}></td> 
    18.     <td width="110" align="right">关键词:</td> 
    19.     <td width="220" align="left"><{$news.keyword}></td> 
    20.   </tr> 
    21.   <tr> 
    22.     <td height="25" align="right" >核心提示:</td> 
    23.     <td height="25" colspan="5" class="artical" style="border:double #FF0000 3px;"><{$news.sumary}></td> 
    24.   </tr> 
    25.   <tr> 
    26.     <td height="25" colspan="6" align="left" class="artical" style="padding:10px;"><{$news.contents}></td> 
    27.   </tr> 
    28. </table> 
    29. </body> 
    30. </html> 
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值