This is implemented directly using the constructor. Considering that there will be a lot of processes that generate static pages, it is better to change it to a function call. This is reserved for future use. In the class, replace template() with templateHtml() to achieve it. ,
This is implemented directly using the constructor. Considering that there will be many processes that generate static pages, it is better to change it to a function call. It is reserved here for backup. Just replace template() in the class with templatehtml(). Realize,
*/
class template
function template()
{
var $temp; //The read template page file name (including path)
var $html; //The name of the html file to be generated (including path)
var $err; var $test; //Data flow
var $arr; having having to replace the array pattern (key name->template label, key value->replacement content)
{
if((int)$err==0)
$this->temp=""; $this->temp="";
}
function templatehtml($temp,$html,$arr)
{
$err=$this->chkfile($temp);{
$test=fread($fp,filesize($temp)); //Read the data stream of the template page
}
echo "Generated from template page ".$temp." ".$html.$this->error($err);
Return;
}
/*
*Check whether the file exists
*Return error message
*/function chkfile($file)
}
{
if (file_exists($file))
{
Return 0;
}
return 1;
}
/*
*According to the content of the array file, replace the data flow pattern (key name->template label, key value->replacement content)
*Return data stream
*Parameter $arr: array
*Parameter $test: data stream
*/
Function arr_replace($arr,$test)
{
$ss=$test;
foreach ($arr as $key => $value)
{
$ss= str_replace($key,$value,$ss);
}
Return $ss;
}
/*
*Write data stream to file
*Return to execution status
*Parameter $html: html file to be generated
*Parameter $test: data stream
*/
Function writefile($html,$test)
{
$stat=2;
If($this->chkfile($html)==0) //Determine whether the file exists
{
$stat=0; //Already exists and returns 0
}
If($f=fopen($html,"w")) //Open the file in writing mode, create it if it does not exist
{
fputs($f,$test);
fclose($f);
}
else
{
$stat=2; //Write failure returns 2
}
Return $stat;
}
/*
*Error message
*Return error message
*Parameter $err: error number
*Parameter $file: error file
*/
function error($err)
{
$message="";
switch((int)$err)
{
case 0 :
$message="Static page generated successfully";
break;
case 1 :
$message=" The template page failed to open, please check whether it exists";
break;
case 2 :
$message="File generation failed, please check directory permissions";
break;
default:
$message="Unknown error";
}
return $message;
}
/*
*
*Mainly used to read template pages and return data streams (such as top, foot public files,)
*Parameter $file: template page path
*/
function readhtml($file)
{
$test="";
$err=$this->chkfile($file);
if($err==0)
{
$fp=fopen($file,"r"); //Open the template page in read-only mode
$test=fread($fp,filesize($file)); //Read the data stream of the template page
}
else
{
$test=$file.$this->error($err);
}
Return $test;
/*
*
*Mainly used to delete generated files without returning
*Parameter $file: file path
*/function delete_file($file)
{
if (file_exists($file))
{
$delete = chmod ($file, 0777);
$delete = unlink($file);
if(file_exists($file))
{
$filesys = eregi_replace("/","",$file);
$delete = system("del $filesys");
clearstatcache();
if(file_exists($file))
{
$delete = chmod ($file, 0777);
$delete = unlink($file);
$delete = system("del $filesys");
}
}
clearstatcache();
}
}
}/*
调用方法demo.html的代码
{title}
{text}
$sc=new template();
//$sc=new template($tmp,$filename,$arr); ()$tmp="../template/temp.php教程"; //模板页
$filename="test.html"; //生成页
$foot="../foot.html"; //包含的底文件,头文件同理$arr=array();
$arr["{title}"]="新标题";
$arr["{text}"]="新内容";
$arr["{foot}"]=$sc->readhtml($foot);
$sc->templatehtml($tmp,$filename,$arr);