Using Libtemplate to realize static Web page Generation _php Foundation

Source: Internet
Author: User

Author: iwind

Originally published in Dev-club an article, how to use template processor Phplib in the Template.inc to achieve static Web page generation, oh, incredibly is included in the essence, and is reproduced by multiple sites, it is an honor to think. In fact, a lot of things on the Internet, I published the so-called Iams (Iwind article management System), there are also, people can see. Below I just briefly summarize at once.

Now generally said there are three ways to generate static Web pages, one is to configure the server, we can go to http://www.devarticles.com/c/b/PHP/to look for, 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 (), open the output buffer, then the data analysis, operation, etc., then use Ob_get_contents (), get the contents of the buffer, and then write to the file. Depending on this step, you can write the following procedure:
<?php
Ob_start ();
Main parts, data manipulation, processing, output, etc...
Require "global.php";
mysql_connect ("localhost", "root", "");
.....
Get buffer contents
$contents =ob_get_contents ();
If you don't want to output anything, you can add this sentence
Ob_end_clean ();
Write to destination file
$fp = @fopen ($targetFile, "w+") or Die ("Error opening file");
Fwrite ($fp, $contents);
?>

In this way, the contents of this dynamic page are written to the static page, $targetFile. Like some of the home page content a lot, to call N multiple query statements, you can periodically generate static Web pages, both greatly improve the speed of access, but also reduce the burden on the server.

As you can see, I use ob_ just to process a single page, and for bulk writing or updating multiple pages, this method is not. That's the third way I'm going to talk, using templates. Template is a good dongdong, now we are more or less in use it, the proposal will not be a simple template processing users, spend some time to learn it, the general template processing procedures are very simple. Using the template to achieve static Web page generation is very simple, the method is to obtain analysis results, the analysis results written to the file. Let's talk about the template.inc in Phplib. If you use a template to generate a static web page.

One, modify Template.inc
Add a few of the following functions:
Save the analysis results to a file
function SaveToFile ($dir, $varname) {
$data = $this->finish ($this->get_var ($varname));
$FP =fopen ($dir, "w+");
Fwrite ($fp, $data);
}
Clearing an array of assigned values
function Renew () {
$this->varkeys=array ();
$this->varvals=array ();
$this->file=array ();
}

The first function is to save the results to a static file, and the second is to leave all the template analysis variables blank to avoid interacting with the batch processing.

Second, the implementation of static Web page generation.
<?php
$ITPL->set_file ("main", "Mian.tpl");
Profiling template Variables
.....
Analysis Mainmains
$tpl->parse ("mains", "main");
Mains the analysis results into main.html
$tpl->savetofile ("main.html", "mains");
Empty
$TPL->renew ()/Critical
?>

Oh, is not very simple, main.html is the content we want. The following is an example of a database combined with a function encapsulated.
<?php
$aid is the article ID in the database, $table is the table name, $template is the template address, $TPL is an instance of the Template.inc
Each aid corresponds to a static Web page address that exists in a datasheet
The structure of the table is similar to the aid target title
1 a1.html ....
2 a2.html ....
3 a3.html ....
function Staticinfo ($aid) {
Global $table, $template, $TPL;
Querying the database
$res =mysql_query ("select * from $table where aid= ' $aid ');
Remove data
$array =mysql_fetch_array ($res);
Read static page address, title.
$target = $array ["Target"];
$title = $array ["title"];
Profiling templates
$TPL->set_file ("main", $template);
Change the {title} variable in the template to $title
$itpl->set_var ("title", $title);
Analyze the entire template
$itpl->set_var ("mains", "main");
Write mains to a file
$tpl->savetofile ($target, "mains");
Empty
$tpl->renew ();
}
?>

So we can use the function staticinfo () to generate a static Web page for any article we want to process. The table $target can also contain the content of the article, author, source, etc., the method is the same.

Third, update static web pages
After an article joins the database, for some reasons we always have to revise some articles. At this time, just the corresponding static Web page can be rebuilt once. This is very handy because the table already has a target field for the destination address of the static Web page.

Can see the key, an article to generate static Web page is the key is $template (template address), $target (destination address). The former, we can make sure that the latter can be at your whim to set an address for each article. Commonly used is 1, time stamp 2, cent seconds 3, according to the article ID. Because these chances of repetition will be very small.

Four, the bulk generation of static Web pages.
With the function of static Web pages generated by a single article, batch generation is very simple. is to get all the articles aid, and then the nested function can be.
<?php
Referencing template classes
Require "template.inc";
Introducing functions
Require "functions.php";
The definition of some variables
$table = "Art";
$template = "TEMPLATE/INFO.TPL";
$TPL =new Template (".");
Connect MySQL, select database
mysql_connect ("localhost", "root", "");
mysql_select_db ("article");
Send Query statement
$res =mysql_query ("Select aid from $table");
while ($r =mysql_fetch_array ($res)) {
$aid = $r ["Aid"];
Generating static Web pages
Staticinfo ($aid);
}
End
echo "All static Web page update/build Success";
?>

The above is the complete example. We do the CMS process can be as follows:
1, press (put the manuscript content into the database)
2, edit review (if he thinks it can be published, then it can generate static web pages)
3, return the manuscript (delete the generated static page, delete the contents of the database)

Then, the content of the website we visited is static. One question is, will this approach take up a lot of space? Http://www.knowsky.com has thousands of articles, taking up only 20M of space. Conversely, if you have 10,000 articles, you are not stingy enough to buy only 200M of space?

Perhaps you are confused about generating a static list of articles, but the way it is is to calculate the page number  analyze the contents of each page  write to the file. Analysis of the content of each page, of course, is to write a function, if you create a page, I am afraid to be ridiculed ^_^.

Static Web pages not only can reduce the burden on the server, improve the speed of access, but also easy to do mirror sites, easy to backup, reduce the amount of damage to attack, speed up the restore speed. Of course, static Web pages will also bring a lot of inconvenience to the place, you need to make a balance between dynamic and static, you can also add in the static page JS call PHP code, to achieve the count, real-time updates and other purposes. (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.