首先說(shuō)原理。某駝查了那么多資料,發(fā)現(xiàn)不管用什么方法,原理都是一樣的。就是用程序讀取相應(yīng)的數(shù)據(jù)來(lái)替換模版中的變量,然后生成靜態(tài)頁(yè)。php中主要用到的就是要用到fread()和fwirte()。而靜態(tài)頁(yè)面生成了之后,就會(huì)牽扯到修改的問(wèn)題。這里可以用到正則匹配的方法來(lái)替換模版中改變的部位。不過(guò)此種方法太麻煩,駝駝推薦的方法是直接把原來(lái)生成的模版砍掉,重新生成,呵呵,真正的一了百了。
還需要說(shuō)明的一點(diǎn)就是,這種生成靜態(tài)頁(yè)面的方法一般都用于那些變化不是很頻繁的頁(yè)面,比如信息的最終頁(yè)面。而針對(duì)列表頁(yè),如果信息更新不是很頻繁的話,也是可取的。現(xiàn)在網(wǎng)上流行好多可以生成靜態(tài)頁(yè)面的blog或者論壇程序,都是通過(guò)手動(dòng)點(diǎn)擊后臺(tái)“生成html頁(yè)”的按鈕來(lái)“半自動(dòng)”生成html的。而對(duì)一些信息量非常大的門戶網(wǎng)站,則行不通。因?yàn)殪o態(tài)頁(yè)之所以叫“靜態(tài)”,是因?yàn)槠洳豢勺詣?dòng)改變。如果信息列表每天更新100次,那么靜態(tài)的列表頁(yè)就要重新生成100次。如果我有10個(gè)這樣的欄目,那想想也夠吐血的了。
好了,閑話少說(shuō),現(xiàn)在來(lái)看看實(shí)際的程序演示:
first:是一個(gè)利用ob函數(shù)來(lái)做的咚咚,代碼比較簡(jiǎn)單,效率相對(duì)也高一些。某駝從某個(gè)高人處得到的源碼,做了一些改動(dòng)
<?php
ob_start();
@readfile("http://localhost/?package=pricab&place_port=4");
$text = ob_get_flush();
$myfile = fopen("myfile.html","w");
$text = str_replace ("{counent}",$string,$text);
fwrite($myfile,$text);
ob_clean();
?>
因?yàn)榫退阋伸o態(tài)頁(yè)面,動(dòng)態(tài)讀取那部分也是要保留的,把數(shù)據(jù)插入數(shù)據(jù)庫(kù)后,把url傳遞給readfile函數(shù),然后讀入緩存,fwrite一下就可以生成靜態(tài)頁(yè)面,這個(gè)是駝駝最欣賞的一種作法。代碼行數(shù)最少,效率最高。駝駝這邊要求http://localhost/?package=pricab&place_port=4是一個(gè)裸頁(yè),也就是單純的內(nèi)容,沒(méi)有頭,尾,菜單。這樣才能比較自由的定制自己的模版myfile.html。如果僅僅是要求生成靜態(tài)頁(yè)的話,
ob_start();
@readfile("http://localhost/?package=pricab&place_port=4");
$string = ob_get_flush();
$myfile = fopen("myfile.html","w");
fwrite($myfile,$string);
ob_clean();
就可以over了
second:普通生成靜態(tài)html頁(yè)。
這種作法就是按部就班的來(lái)做,fread進(jìn)來(lái)頁(yè)面,然后str_replace替換
首先是創(chuàng)建最終內(nèi)容頁(yè):
$title = "http://siyizhu.com測(cè)試模板";
$file = "TwoMax Inter test templet,<br>author:Matrix@Two_Max";
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content = str_replace ("{file}",$file,$content);
$content = str_replace ("{title}",$title,$content);
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //打開(kāi)文件指針,創(chuàng)建文件
/*
檢查文件是否被創(chuàng)建且可寫
*/
if (!is_writable ($filename)){
die ("文件:".$filename."不可寫,請(qǐng)檢查其屬性后重試!");
}
if (!fwrite ($handle,$content)){ //將信息寫入文件
die ("生成文件".$filename."失敗!");
}
fclose ($handle); //關(guān)閉指針
die ("創(chuàng)建文件".$filename."成功!");
這一步比較簡(jiǎn)單。只是單純的變量替換即可。如果要生成靜態(tài)的列表頁(yè)面的話,原理也是一樣,用程序來(lái)生成文章列表,把它當(dāng)成一個(gè)大的變量,替換模版中的變量,列表的翻頁(yè)頁(yè)是如此。當(dāng)然,如果有信息更新的話,列表翻頁(yè)也是要重新生成的。
<?php
$title = "http://";
$file = "TwoMax Inter test templet,<br>author:Matrix@Two_Max";
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$content = str_replace ("{file}",$file,$content);
$content = str_replace ("{title}",$title,$content);
// 生成列表開(kāi)始
$list = '';
$sql = "select id,title,filename from article";
$query = mysql_query ($sql);
while ($result = mysql_fetch_array ($query)){
$list .= '<a href='.$root.$result['filename'].' target=_blank>'.$result['title'].'</a><br>';
}
$content .= str_replace ("{articletable}",$list,$content);
//生成列表結(jié)束
// echo $content;
$filename = "test/test.html";
$handle = fopen ($filename,"w"); //打開(kāi)文件指針,創(chuàng)建文件
/*
檢查文件是否被創(chuàng)建且可寫
*/
if (!is_writable ($filename)){
die ("文件:".$filename."不可寫,請(qǐng)檢查其屬性后重試!");
}
if (!fwrite ($handle,$content)){ //將信息寫入文件
die ("生成文件".$filename."失敗!");
}
fclose ($handle); //關(guān)閉指針
die ("創(chuàng)建文件".$filename."成功!");
?>
關(guān)于翻頁(yè):
如我們指定分頁(yè)時(shí),每頁(yè)20篇。某子頻道列表內(nèi)文章經(jīng)數(shù)據(jù)庫(kù)查詢?yōu)?5條,則,首先我們通過(guò)查詢得到如下參數(shù):1,總頁(yè)數(shù);2,每頁(yè)篇數(shù)。第二步,for ($i = 0; $i < allpages; $i++),頁(yè)面元素獲取,分析,文章生成,都在此循環(huán)中執(zhí)行。不同的是,die ("創(chuàng)建文件".$filename."成功!";這句去掉,放到循環(huán)后的顯示,因?yàn)樵撜Z(yǔ)句將中止程序執(zhí)行。例:
<?php
$fp = fopen ("temp.html","r");
$content = fread ($fp,filesize ("temp.html"));
$onepage = '20';
$sql = "select id from article where channel='$channelid'";
$query = mysql_query ($sql);
$num = mysql_num_rows ($query);
$allpages = ceil ($num / $onepage);
for ($i = 0;$i<$allpages; $i++){
if ($i == 0){
$indexpath = "index.html";
} else {
$indexpath = "index_".$i."html";
}
$start = $i * $onepage;
$list = '';
$sql_for_page = "select name,filename,title from article where channel='$channelid' limit $start,$onepage";
$query_for_page = mysql_query ($sql_for_page);
while ($result = $query_for_page){
$list .= '<a href='.$root.$result['filename'].' target=_blank>'.$title.'</a><br>';
}
$content = str_replace ("{articletable}",$list,$content);
if (is_file ($indexpath)){
@unlink ($indexpath); //若文件已存在,則刪除
}
$handle = fopen ($indexpath,"w"); //打開(kāi)文件指針,創(chuàng)建文件
/*
檢查文件是否被創(chuàng)建且可寫
*/
if (!is_writable ($indexpath)){
echo "文件:".$indexpath."不可寫,請(qǐng)檢查其屬性后重試!"; //修改為echo
}
if (!fwrite ($handle,$content)){ //將信息寫入文件
echo "生成文件".$indexpath."失敗!"; //修改為echo
}
fclose ($handle); //關(guān)閉指針
}
fclose ($fp);
die ("生成分頁(yè)文件完成,如生成不完全,請(qǐng)檢查文件權(quán)限系統(tǒng)后重新生成!");
?>
third:smarty模版生成靜態(tài)頁(yè)面
駝駝是用smarty模版的,smarty自己有一個(gè)fetch函數(shù),其功用有點(diǎn)類似于fread()可以用來(lái)生成靜態(tài)的頁(yè)面.
這個(gè)例子大家想必看起來(lái)眼熟,對(duì),smarty手冊(cè)中關(guān)于fetch函數(shù)的例子,hoho 某駝借用一下,比竟官方的例子總是很經(jīng)典的嘛!
<?php
include("Smarty.class.php");
$smarty = new Smarty;
$smarty->caching = true;
// only do db calls if cache doesn't exist
if(!$smarty->is_cached("index.tpl")) {
// dummy up some data
$address = "245 N 50th";
$db_data = array(
"City" => "Lincoln",
"State" => "Nebraska",
"Zip" => "68502"
);
$smarty->assign("Name","Fred");
$smarty->assign("Address",$address);
$smarty->assign($db_data);
}
// capture the output
$output = $smarty->fetch("index.tpl"); //這個(gè)地方算是關(guān)鍵
// do something with $output here
echo $output; //hoho 看到output的結(jié)果了吧 然后呢?fwrite一下,我們就得到我們所要的結(jié)果了。
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>
?>
<?php
ob_start();
echo "Hello World!";
$content = ob_get_contents();//取得php頁(yè)面輸出的全部?jī)?nèi)容
$fp = fopen("archives/2005/05/19/0001.html", "w");
fwrite($fp, $content);
fclose($fp);
?>
新聞熱點(diǎn)
疑難解答