Home > Backend Development > PHP Tutorial > Smarty中批量生成html的方法

Smarty中批量生成html的方法

WBOY
Release: 2016-05-19 15:12:11
Original
1014 people have browsed it

经过一番摸索,终于找到方法,写出来大家指点一下:
首先在smartt.class.php中为Smarty类添加这么一个方法:
PHP代码:
//参数一:html文件保存路径,参数2:写入的内容
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);  
  }


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

我们先看看news.php
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");
?>


整个模板变量就只有$news,这么写有什么好处呢?我可以以数组的方式读取内容

那么究竟如何生成:
看如下代码:
很简单的
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));
}
?>


很简单把!


PHP代码:









 
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template