Using Libtemplate to implement static Web page generation _php Tutorial

Source: Internet
Author: User
Author: iwind

Originally published in Dev-club an article, how to use the template processing program Phplib in the Template.inc to achieve static web page generation, hehe, incredibly be included in the essence, and was reproduced by a number of sites, want to think it is an honor. In fact, this aspect of the Internet a lot of things, I published the so-called Iams (Iwind article management System), there are, people can look. Below I just briefly summarize once.

Now generally say there are three ways to generate static Web pages, one is to configure the server, you can go to http://www.devarticles.com/c/b/PHP/to look for, for this many places have. The other one is to control the output with the Ob_ function. The method is as follows: First use Ob_start (), open the output buffer, then the analysis of the data, operation, and so on, followed by ob_get_contents (); Gets the contents of the buffer and then writes the file. According to this procedure, you can write the following program:
Ob_start ();
Body parts, data manipulation, processing, output, etc...
Require "global.php";
mysql_connect ("localhost", "root", "");
.....
Get buffer contents
$contents =ob_get_contents ();
If you do not want to output anything, you can add this sentence
Ob_end_clean ();
Write destination file
$fp = @fopen ($targetFile, "w+") or Die ("Error opening file");
Fwrite ($fp, $contents);
?>

This will write the content of this dynamic page to a static page, $targetFile. Like some Web site home page content, to invoke n multiple query statements, you may wish to generate static Web pages, not only greatly improve the access speed, but also reduce the burden of the server.

As you can see, I use ob_ just to handle a single page, which is not a method for bulk writing or updating multiple pages. That's the third way I'm going to talk about this, using templates. Template is a good thing, now we are more or less in use it, the proposal will not be simple template processing of netizens, take some time to learn it, the general template processing procedures are very simple. It is very simple to generate static Web pages with templates by getting the results of the analysis and writing the results 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 several of the following functions:
Save the results of the analysis to a file
function SaveToFile ($dir, $varname) {
$data = $this->finish ($this->get_var ($varname));
$FP =fopen ($dir, "w+");
Fwrite ($fp, $data);
}
Clears an array of values that have been assigned
function Renew () {
$this->varkeys=array ();
$this->varvals=array ();
$this->file=array ();
}

The first function is to save the result to a static file, the second is to set all the template analysis variables to null, so as not to interfere with the batch processing.

Second, the realization of static Web page generation.
$ITPL->set_file ("main", "Mian.tpl");
Analyze Template variables
.....
Analysis Mainmains
$tpl->parse ("mains", "main");
Mains the results of the analysis into main.html
$tpl->savetofile ("main.html", "mains");
Empty
$tpl->renew ();//Critical
?>

Oh, is not very simple, main.html is the content we want. Here is an example that combines a database and encapsulates it with a function.
$aid is the article ID in the database, $table is the name of the table, $template is the template address, $TPL is an instance of Template.inc
Each aid corresponds to a static web address that exists in a data table
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 web address, title.
$target = $array ["Target"];
$title = $array ["title"];
Analysis 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 File
$tpl->savetofile ($target, "mains");
Empty
$tpl->renew ();
}
?>

So we can use the function staticinfo () to generate static pages for any article we want to work with. Table $target can also contain article content, author, source and so on, the method is the same.

Third, update the static Web page
After an article has been added to the database, there are some reasons why we have to revise some articles. At this point, simply regenerate the corresponding static Web page once. This is very handy because the table already has the destination address target field for the static Web page.

As you can see, the key to generating static pages in an article is $template (template address), $target (target address). The former, we can first determine, the latter can be your arbitrary for each article set an address. Commonly used there are 1, timestamp 2, time Division 3, according to the article ID. Because the chances of repetition are very small.

Generate static Web pages in bulk.
With the function of a static web page generated by a single article, batch generation is very simple. is to get all the article aid, and then the function can be nested.
Referencing template classes
Require "template.inc";
Introducing functions
Require "functions.php";
Definition of some variables
$table = "Art";
$template = "TEMPLATE/INFO.TPL";
$TPL =new Template (".");
Connect to MySQL, select a 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"];
Generate a static web page
Staticinfo ($aid);
}
End
echo "All static Web pages updated/generated successfully";
?>

The above is a complete example. The process of our CMS can be as follows:
1, press release (the contents of the manuscript into the database)
2, editorial review (if he thinks it can be published, then you can generate static Web pages)
3, return the manuscript (delete the generated static page, delete the contents of the database)

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

Perhaps you are confused about generating static list of articles, in fact the method is the same, that is, calculate page number  analyze the content of each page to write to the file. Analysis of the content of each page, of course, is to write a function, if you create a page by page, I am afraid that people laughed ^_^.

Static Web pages can not only reduce the burden of the server, improve access speed, but also easy to do mirror site, easy to backup, reduce the level of attack loss, speed up the restore speed. Of course, static Web pages will also bring you a lot of inconvenience, you need to balance between dynamic and static, you can also add JS call PHP code in static Web pages, to achieve the count, immediate updating and so on. Finish

http://www.bkjia.com/PHPjc/314562.html www.bkjia.com true http://www.bkjia.com/PHPjc/314562.html techarticle Author: Iwind Originally published in Dev-club an article, how to use the template processing program Phplib in the Template.inc to achieve static web page generation, hehe, incredibly be included in the essence, and be more ...

  • 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.