How to generate static web pages through smarty

Source: Internet
Author: User

The biggest function of Smarty is to cache pages of templates. 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;
// & Hellip; Omitting variable definition and assignment
// $ Smarty-> display ('index. tpl ');
$ Content = $ smarty-> fetch ("index. tpl ");
$ Smarty-> MakeHtmlFile ('./index.html', $ content); // generate

From the PainsOnline Column

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.