Two methods for generating html static pages using php

Source: Internet
Author: User
Tags php template
In the articles I have seen earlier, if it is not to use code to build a space, it is to use the language used by experts to communicate with experts to let new people look at it. Therefore, this article tries to elaborate on the overall idea. the two methods are described as follows: 1. use the output control function (OutputContr... in the articles I have seen earlier, if it is not to use code to build a space, it is to use the language used by experts to communicate with experts to let new people look at it. Therefore, this article tries to elaborate on the overall idea.

The two methods are described as follows:

1. use the Output Control function of PHP to obtain the static page string and then write it into the new file.

Instructions for use:

1. instantiate the code as follows:

$ Cache = new Cache ();

2. set cache time and cache Directory

$ Cache = new Cache (60, '/any_other_path /');

The first parameter is the cache seconds, and the second parameter is the cache path. it is configured as needed. by default, the cache time is 3600 seconds, and the cache directory is cache /.

3. Read the cache. the code is as follows:

 Get ('data _ key'); 4. write cache $ value = $ cache-> put ('data _ key', 'data _ value'); complete instance: $ cache = new Cache (); // read key value $ key data from the cache $ values = $ cache-> get ($ key ); // if no data is cached if ($ values = false) {// insert code here... // write data with the key value $ key $ cache-> put ($ key, $ values);} else {// insert code here...}?>

Cache. class. php

 Cache_expire = $ exp_time; $ this-> cache_path = $ path;} // returns the filename for the cache private function fileName ($ key) {return $ this-> cache_path. md5 ($ key) ;}// creates new cache files with the given data, $ key = name of the cache, data the info/values to store public function put ($ key, $ data) {$ values = serialize ($ data); $ filename = $ this-> fileName ($ key ); $ file = fopen ($ filename, 'w'); if ($ File) {// able to create the file fwrite ($ file, $ values); fclose ($ file);} else return false ;} // returns cache for the given key public function get ($ key) {$ filename = $ this-> fileName ($ key); if (! File_exists ($ filename) |! Is_readable ($ filename) {// can't read the cache return false;} if (time () <(filemtime ($ filename) + $ this-> cache_expire )) {// cache for the key not expired $ file = fopen ($ filename, "r"); // read data file if ($ file) {// able to open the file $ data = fread ($ file, filesize ($ filename); fclose ($ file); return unserialize ($ data ); // return the values} // open-source code phprm.com else return false;} else return f Alse; // was expired you need to create new }}?>

II. generate a template

What is a template? If you have used "Save as template" in Dreamwerver, you should know that the template is used to unify the style. it only allows you to modify a part of the page, of course, this "part" is determined by you. the template mentioned in this article also means this. In addition, the PHP template technology also includes phplib and smarty, this is not the content of this article.

Combining the concept of a template with this article, let's talk about the specific point: the artist first makes a page, then we regard this page as a template (it should be noted that this template does not need to use code such as EditRegion3, which is the identifier that Dreamwerver gets to facilitate its own design ), replace the template with a character that can be distinguished from HTML, such as "{title}" and "[title]". When generating a static page, you only need to replace the data with these strings. This is the meaning of the template.

Steps:

1. create a php page and an html page [template page]. note: if data is called from the database, the data is saved as an array and generated cyclically;

2. on the php page, open the html page-> read the content of the html page-> replace the parameter-> Create (open) A new html page> write the replaced content to the new file> close the new file> generate the file successfully. the code is as follows:

 $ Value) {$ title = $ value [0]; $ contents = $ value [1]; // echo $ title. ''. $ contents. ''; $ path = $ key. '.html '; $ open = fopen ("template.htm", "r"); // open the template file $ handle = fread ($ open, filesize ("template.htm ")); // read the template file content $ content = str_replace ("{title}", $ title, $ handle); // replace $ content = str_replace ("{contents }", $ contents, $ handle); $ newtemp = fopen ($ path, "w"); // open a non-existing (new) page fwrite ($ newtemp, $ con Tent); // write the replaced content to the new file fclose ($ newtemp); echo "generated";}?>


Tutorial address:

Reprinted! But please include the article address ^

Related Article

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.