Two methods for generating html static pages using php

Source: Internet
Author: User

In my previous articles, if I didn't use code to build a space, I would just use the language that experts use to communicate with experts to attract new people. 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. instantiation

The Code is as follows: Copy code

$ Cache = new Cache (); 2. Set the 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, which can be configured as needed.
By default, the cache duration is 3600 seconds, and the cache directory is cache/

3. Read Cache

The Code is as follows: Copy code

$ Value = $ cache-> get ('data _ key'); 4. Write to 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

<? Php
Class Cache {
Private $ cache_path; // path for the cache
Private $ cache_expire; // seconds that the cache expires

// Cache constructor, optional expiring time and cache path
Public function Cache ($ exp_time = 3600, $ path = "cache /"){
$ This-> 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
}
Else return false;
}
Else return false; // 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, PHP template technology also includes phplib, smarty, and so on. This is not the content mentioned in 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 successfully;

The Code is as follows: Copy code

$ Open = fopen ("template.htm", "r"); // open the template file.
$ Content = fread ($ open, filesize ("template.htm"); // read the Template File content
// Print_r ($ content );
$ Content = str_replace ("{title}", "test title", $ content); // replace
$ Content = str_replace ("{contents}", "Test content", $ content );

$ Newtemp = fopen ("1.htm"," w "); // generate a page that does not exist (new) in writing mode.
Fwrite ($ newtemp, $ content); // write the replaced content to the new file.
Fclose ($ newtemp );
Echo "generate ";

Php batch generate html test:

The Code is as follows: Copy code

// Assume that the data transferred from the database is stored in a two-dimensional array $ arr.
$ Arr = array ("News Title 1", "news content 1"), array ("News Title 2", "news content 2 "));

Foreach ($ arr as $ key => $ value ){
$ Title = $ value [0];
$ Contents = $ value [1];
// Echo $ title. ''. $ contents .'';
$ Path = export 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"); // use the write method to open a page that does not exist (new ).
Fwrite ($ newtemp, $ content); // write the replaced content to the new file.
Fclose ($ newtemp );
Echo "generate ";
}


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.