The biggest function of Smarty is to cache template pages. That is to say, two steps can be completed through Smarty: compilation + analysis
Step one: compile. It means to replace the tag of the template file with pure PHP, and then save it in the cache location. The saved file extension is PHP. I call this step compilation (this is my own name, not official)
Step two: analysis. That is to say, it just parses and executes the PHP file you just compiled~~ No need to explain this
Let’s get to the point and add the following code to the Smarty.class.php file
function MakeHtmlFile($file_name, $content)
{ //Create the directory if it does not exist
If (!file_exists (dirname($file_name))) {
If (!@mkdir (dirname($file_name), 0777)) {
die($file_name."Directory creation failed!");
}
}
If(!$fp = fopen($file_name, "w")){
echo "File opening failed!";
return false;
}
If(!fwrite($fp, $content)){
echo "File writing failed!";
fclose($fp);
return false;
}
fclose($fp);
chmod($file_name,0666);
}
The function of this function is to save the file~~
The calling method is as follows
require '../libs/Smarty.class.php';
$smarty = new Smarty;
//…………Omit variable definition and assignment
//$smarty->display('index.tpl');
$content=$smarty->fetch("index.tpl");
$smarty->MakeHtmlFile('./index.html',$content);//Generate
Excerpted from PainsOnline’s column
http://www.bkjia.com/PHPjc/478329.html