SoFunction
Updated on 2025-04-13

Use libTemplate to achieve the generation of static web pages

Author: iwind

It turns out that an article published in dev-club will use the template handler PHPlib to achieve the generation of static web pages. Haha, it was actually included in the essence and reprinted by multiple websites. I think this is an honor. In fact, there are many things on the Internet. The so-called IAMS (iwind article management system) I published here also has it. Those who are interested can take a look. I'll just briefly summarize it.

Now generally speaking, there are three ways to generate static web pages. One is to configure the server. You can search in /c/b/PHP/, which are available in many places. 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, then analyze the data, operate, etc., then use ob_get_contents(); to obtain the contents of the buffer, and then write to the file. According to this step, you can write the following program:
   <?php
   ob_start();
//Main part, data operations, processing, output, etc. . .
   require””;
   mysql_connect(“localhost”,”root”,””);
   …..
//Get buffer content
   $contents=ob_get_contents();
//If you don't want to output anything, you can add this sentence
   ob_end_clean();
//Write to the destination file
$fp=@fopen($targetFile,"w+") or die("Error opening the file");
   fwrite($fp,$contents);
?>

This way, the content of this dynamic page is written into the static page, $targetFile. For example, some websites have a lot of content on the homepage. When calling n multiple query statements, it is better to generate static web pages regularly, which not only greatly improves the access speed, but also reduces the burden on the server.

As you can see, I use ob_ to just process a single page, and this method won't work for batch writing or updating multiple pages. This is the third method I want to talk about, using templates. Template is a good thing. Now everyone is using it more or less. It is recommended that netizens who don’t know how to process templates simply, take some time to learn it. The general template processing procedures are very simple. It is very simple to use templates to generate static web pages. The method is to obtain the analysis results and write the analysis results to a file. Let’s talk about using the one in PHPlib to create a static web page.

1, Modify
Add the following functions:
//Save the analysis results to the 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 results in a static file, and the second is to set all template analysis variables to empty to avoid mutual influence during batch processing.

2. Realize static web page generation.
<?php
$itpl->set_file(“main”,””);
//Analyze template variables
…..
//Analyze mainmains
$tpl->parse("mains","main");
//Save the analysis results mains in
$tpl->savetofile("","mains");
//Empty
$tpl->renew();// is crucial
?>

Haha, isn’t it very simple? It’s what we want. The following is an example of combining databases and encapsulated with functions.
<?php
//$aid is the article id in the database, $table is the table name, $template is the template address, and $tpl is an instance
//Each Aid corresponds to a static web page address, and it exists in a data table
//The structure of the table is similar to aid    target      title
//                1          ….
//                2          ….
//                3          ….
function staticInfo($aid){
   global $table,$template,$tpl;
//Query the database
   $res=mysql_query(“select * from $table where aid=’$aid’”);
//Fetch the data
   $array=mysql_fetch_array($res);
//Read the static web page address and title.
   $target=$array[“target”];
$title=$array[“title”];
//Analysis template
$tpl->set_file(“main”,$template);
//Swap 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();
}
?>

In this way, we can use the function staticInfo() to generate a static web page for any article we want to process. Table $target can also contain article content, author, source, etc., the method is the same.

3. Update static web pages
After an article is added to the database, we always need to modify some articles for some reasons. At this time, just regenerate the corresponding static web page once. This is very convenient because the target address of the static web page is already in the table.

It can be seen that the key lies in the key to generating a static web page in an article is $template (template address) and $target (target address). We can first determine the former, and the latter can set an address for each article at your own discretion. Commonly used are 1, timestamp 2, hours, minutes and seconds 3, according to the article id. Because the chances of these repetitions will be very small.

4. Bulk generation of static web pages.
With the function of static web pages generated by a single article, batch generation is very simple. It is to obtain all the article aids and then insert the functions.
<?php
//Reference template class
require””;
//Introduce functions
require””;
//Definition of some variables
$table=”art”;
$template=”template/”;
$tpl=new Template(“.”);
//Connect mysql and select the 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 static web page
   staticInfo($aid);
}
//Finish
echo "All static web pages are updated/generated successfully";
?>

The above is a complete example. Our process of doing cms can be as follows:
1. Reporter publishes articles (put the content of the manuscript into the database)
2. Editor review (if he thinks it can be published, then he can generate static web pages)
3. Return the manuscript (delete the generated static web page and delete the content in the database)

Then, the content of the website we visit is static. One question is, does this method take up a lot of space? There are thousands of articles, which only occupy 20M space. On the other hand, if you have 10,000 articles, you won’t be so stingy that you only buy 200M of space, right?

Maybe you are confused about generating a static article list, but in fact the method is the same, which is to calculate the page number  Analyze the content of each page number  Write to the file. To analyze the content of each page number, of course, it is to write a function. If you generate page by page, you may be ridiculed ^_^.

Static web pages can not only reduce the burden on the server and increase access speed, but also make mirrored websites conveniently, backup them easily, reduce the degree of loss caused by attacks, and speed up the recovery speed. Of course, static web pages will also bring you many inconveniences. You need to balance dynamics and statics, and you can also add php code called by js to the static web page to achieve the purpose of counting, instant updates, etc. (over)