How to generate static web pages through smarty. The biggest function of Smarty is to cache pages of templates. That is, two steps can be completed through Smarty: Compilation + parsing Step 1: compilation. It refers to replacing the tag of the template file with pure php, and the biggest function of Smarty is to cache the page of the template. That is, two steps can be completed through Smarty: Compilation + parsing.
Step 1: compile. It refers to replacing the tag of the template file with pure php, and then saving the cache location. the saved file extension is PHP. I call this step compilation (this is my own name, not official)
Step 2: Parse. That is, the PHP file just compiled is parsed and executed ~~ This does not need to be explained.
Add the following code to the Smarty. class. php file.
Function MakeHtmlFile ($ file_name, $ content)
{// Create if the directory 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 );
}
This function is used to save files ~~
The call method is as follows:
Require '../libs/Smarty. class. php ';
$ Smarty = new Smarty;
//............ Omitting variable definitions and values
// $ Smarty-> display ('index. tpl ');
$ Content = $ smarty-> fetch ("index. tpl ");
$ Smarty-> MakeHtmlFile ('./index.html', $ content); // Generate
From the PainsOnline column
Bytes. That is, two steps can be completed through Smarty: Compilation + parsing Step 1: compilation. It refers to replacing the tag of the template file with pure php, and then...