Use libTemplate to generate static webpages

Source: Internet
Author: User
Use libTemplate to generate static web pages: iwind

How to use the template in the template processing program PHPlib in the original article published in dev-club. inc is used to generate static web pages. it is an honor to be included in the essence and reproduced by multiple websites. In fact, there are a lot of things in this regard on the internet. The so-called IAMS (iwind article Management System) I published here also contains, you can take a look. The following is a brief summary.

Now it is generally said that there are three ways to generate static web pages, one is to configure the server, you can go to the http://www.devarticles.com/c/ B /PHP/ to find a look, for this many places have. The other is to use the ob _ function to control the output. The method is as follows: first use ob_start (); to open the output buffer, and then analyze and operate the data. then use ob_get_contents (); to obtain the buffer content and then write it to the file. Follow these steps to write the following program:
Ob_start ();
// Subject, data operations, processing, and output...
Require "global. php ";
Mysql_connect ("localhost", "root ","");
.....
// Obtain the buffer content
$ Contents = ob_get_contents ();
// Add this sentence if you do not want to output anything.
Ob_end_clean ();
// Write the target file
$ Fp = @ fopen ($ targetFile, "w +") or die ("An error occurred while opening the file ");
Fwrite ($ fp, $ contents );
?>

In this way, the content of the dynamic page is written to the static page, $ targetFile. some websites have a lot of homepage content. when calling n multiple query statements, you may wish to periodically generate static webpages, which greatly improves the access speed and reduces the server load.

As you can see, I only use ob _ to process a single page. this method won't work for writing or updating multiple pages in batches. This is the third method I want to talk about. use a template. The template is a good stuff. now everyone is using it more or less. we recommend that you do not use a simple template to learn it. the general template processing procedures are very simple. Using templates to generate static web pages is very simple. the method is to obtain the analysis results and write the analysis results into a file. The following uses template. inc in PHPlib to talk about generating static webpages using templates.

1. modify template. inc.
Add the following functions:
// Save the analysis result to a file
Function savetofile ($ dir, $ varname ){
$ Data = $ this-> finish ($ this-> get_var ($ varname ));
$ Fp = fopen ($ dir, "w + ");
Fwrite ($ fp, $ data );
}
// Clear the assigned array
Function renew (){
$ This-> varkeys = array ();
$ This-> varvals = array ();
$ This-> file = array ();
}

The first function is to save the result to a static file, and the second is to leave all the template analysis variables empty to avoid mutual impact during batch processing.

2. generate static webpages.
$ Itpl-> set_file ("main", "mian. tpl ");
// Analyze Template variables
.....
// Analyze main queue mains
$ Tpl-> parse ("mains", "main ");
// Store the analysis result mainsinto main.html
$ Tpl-> savetofile ("main.html", "mains ");
// Leave it empty
$ Tpl-> renew (); // crucial
?>

It is not very simple, and main.html is what we want. The following is an example of combining databases and encapsulating them with functions.
// $ Aid is the document id in the database, $ table is the table name, $ template is the template address, and $ tpl is an instance of template. inc.
// Each aid corresponds to a static webpage address, which exists in a data table.
// The table structure is similar to aid target title
// 1 a1.html ....
// 2 a2.html ....
// 3 a3.html ....
Function staticInfo ($ aid ){
Global $ table, $ template, $ tpl;
// Query the database
$ Res = mysql_query ("select * from $ table where aid = '$ aid '");
// Retrieve data
$ Array = mysql_fetch_array ($ res );
// Read the static webpage address and its title.
$ Target = $ array ["target"];
$ Title = $ array ["title"];
// Analysis template
$ Tpl-> set_file ("main", $ template );
// Replace the {title} variable in the template with $ title
$ Itpl-> set_var ("title", $ title ");
// Analyze the entire template
$ Itpl-> set_var ("mains", "main ");
// Write the mains into a file
$ Tpl-> savetofile ($ target, "mains ");
// Leave it empty
$ Tpl-> renew ();
}
?>

In this way, we can use the staticInfo () function to generate a static webpage for any article we want to process. The table $ target can also contain the article content, author, source, and so on. The methods are the same.

3. update static webpages
After an article is added to the database, we always need to modify some articles for some reasons. At this time, you only need to regenerate the corresponding static webpage. This is very convenient because the target Field of the static webpage already exists in the table.

As you can see, the key to generating a static webpage in an article is $ template and $ target ). For the former, we can determine first, and for the latter, you can set an address for each article as you like. Commonly used are 1, 2 timestamp, and 3 seconds, based on the article id. This is because the chances of repetition are very small.

4. generate static web pages in batches.
With the static web page function generated by a single article, batch generation is very simple. Is to get all the article aid, and then set the function.
// Reference template class
Require "template. inc ";
// Introduce the function
Require "functions. php ";
// Definition of some variables
$ Table = "art ";
$ Template = "template/info. tpl ";
$ Tpl = new Template (".");
// Connect to mysql and select database
Mysql_connect ("localhost", "root ',"");
Mysql_select_db ("article ");
// Send the query statement
$ Res = mysql_query ("select aid from $ table ");
While ($ r = mysql_fetch_array ($ res )){
$ Aid = $ r ["aid"];
// Generate a static webpage
StaticInfo ($ aid );
}
// End
Echo "all static webpages are updated/generated successfully ";
?>

The above is a complete example. The cms process can be as follows:
1. the reporter issues the manuscript (put the manuscript content into the database)
2. edit the review (if he thinks it can be published, the content can be generated as a static webpage)
3. return the manuscript (delete the generated static webpage and delete the content in the database)

Then, the content of the website we visited is static. One problem is, will this method occupy a lot of space? Http://www.knowsky.com has thousands of articles, only occupied 20 m space. On the other hand, if you have 10000 articles, you will not be mean to only buy MB of space?

Maybe you are confused about generating a static article list. In fact, the method is the same: calculate the page number, analyze the content of each page number, and then write it to the file. To analyze the content of each page number, of course, it is to write a function. if you generate one page at a time, I am afraid it will be ridiculed.

Static web pages not only reduce the burden on servers, increase the access speed, but also facilitate website mirroring, facilitating backup, reducing the damage caused by attacks, and accelerating the restoration speed. Of course, static web pages will bring a lot of inconvenience to everyone. you need to balance between dynamic and static pages, or you can add php code called by js to static web pages to achieve counting, real-time update. (End)

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.