[php] smarty批量生成静态页面

第一个文件:add_html.php

require_once('config.php');//这里是你的SMARTY配置文件

$num=3;//生成静态页面的数量
$title=array("第一个标题","第二个标题","第三个标题");
$contents=array("第一个静态页面内容容","第二个静态页面内容","第三个静态页面内容");
//这里为了方便测试。静态页面内容就直接给出、、


$count_num_html=array();
for($i=0;$i<$num;$i++){
    
    $count_num_html["title"]=$title[$i];
    $count_num_html["contents"]=$contents[$i];

$smarty->assign("title",$count_num_html["title"]);
$smarty->assign("contents",$count_num_html["contents"]);

$new_contents=$smarty->fetch("html.tpl");//截获模板返回的内容。。
//开始生成静态页面,主要就是文件的写入操作
$path=($i+1)."_html.html";//生成静态页面的路径
$fp=fopen($path,'w');
fwrite($fp,$new_contents);//写入刚才截获的内容
fclose($fp);
//这里差不多完成了。。。
}
$smarty->display('html.tpl');//这个可以有也可以没有,主要是执行PHP文件之后能看到显示

======================我是分割线=========================
第二个文件 :html.tpl//放在TEMPLATES目录的模板文件

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><{$title}></title>
</head>

<body>
标题:<{$title}>
<br/>
内容:<{$contents}>
<br/>

</body>
</html>

以上文件通过本人测试生成了1_html.html, 2_html.html,3_html.html三个页面、、、

 

 

 

 

 

posted on 2013-06-17 21:16  似水流年.ag  阅读(577)  评论(0)    收藏  举报

导航