$con=array(array(‘新闻标题’,’新闻内容’),array(‘新闻标题2′,’新闻内容2’));
foreach($con as $id=>$val){
$title=$val[0];
$content=$val[1];
$path=$id.’.htm’;
$fp=fopen(“tmp.htm”,”r”); //只读打开模板
$str=fread($fp,filesize(“tmp.htm”));//读取模板中内容
$str=str_replace(“{title}”,$title,$str);
$str=str_replace(“{content}”,$content,$str);//替换内容
fclose($fp);
$title=$val[0];
$content=$val[1];
$path=$id.’.htm’;
$fp=fopen(“tmp.htm”,”r”); //只读打开模板
$str=fread($fp,filesize(“tmp.htm”));//读取模板中内容
$str=str_replace(“{title}”,$title,$str);
$str=str_replace(“{content}”,$content,$str);//替换内容
fclose($fp);
$handle=fopen($path,”w”); //写入方式打开新闻路径
fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
fclose($handle);
echo “生成成功”;
}
// unlink($path); //删除文件smarty模板生成方法:
<?php
/*
file:config_smarty.php
done:配置Smarty
author:www.5dkx.com
date:2009-12-21
*/
include_once(“../libs/smarty.class.php”);
class MySmarty extends Smarty{
function __construct()
{
$this->Smarty();
$this->config_dir = “../config”;
$this->cache_dir = “../cache”;
$this->template_dir = “../template”;
$this->compile_dir = “../template_c”;
$this->cache = false;
}
}
$smart = new MySmarty();
$smart->assign(“title”,”标题”);
$smart->assign(“content”,”内容”);
$smart->display(‘article.tpl’);
$output = $smart->fetch(‘article.tpl’);
$Path = “../html/1.html”;
$fp = fopen($Path,”w”);
fwrite($fp,$output);
fclose($fp);
?>
fwrite($handle,$str); //把刚才替换的内容写进生成的HTML文件
fclose($handle);
echo “生成成功”;
}
// unlink($path); //删除文件smarty模板生成方法:
<?php
/*
file:config_smarty.php
done:配置Smarty
author:www.5dkx.com
date:2009-12-21
*/
include_once(“../libs/smarty.class.php”);
class MySmarty extends Smarty{
function __construct()
{
$this->Smarty();
$this->config_dir = “../config”;
$this->cache_dir = “../cache”;
$this->template_dir = “../template”;
$this->compile_dir = “../template_c”;
$this->cache = false;
}
}
$smart = new MySmarty();
$smart->assign(“title”,”标题”);
$smart->assign(“content”,”内容”);
$smart->display(‘article.tpl’);
$output = $smart->fetch(‘article.tpl’);
$Path = “../html/1.html”;
$fp = fopen($Path,”w”);
fwrite($fp,$output);
fclose($fp);
?>
JL后记:这个源码我没有经过测试,如果大家测试有问题的话烦请留言说声,因为WORDPRESS里面有强大的插件功能,有PHP转化成静态的HTML文件,所以等需要用的时候我再测试下。