[转] Realize the generation of static web pages with libtemplate

xiaoxiao2021-03-06  41

// from: http://dev.9cbs.net/develop/article/26/26184.shtm Author: iwind in dev-club had published an article, how to achieve the template handler PHPlib in template.inc The generation of static webpage, huh, huh, actually being included in the essence, and is reproduced by multiple websites, and this is honored. In fact, there are many things on the Internet. I have the so-called IAMS (IWind article management system) released, and some people can look at it. Below I just briefly summarized once. Nowadays, there are three ways to generate a static web page, one is to configure the server, you can go to http://www.devarticles.com/c/b/php/ to find it, for this many places. The other is to control the output with an OB_ function. The method is as follows: first use ob_start (); turn on the output buffer, then the analysis, operation, etc. According to this step, the following procedures can be written: This will The contents of the page are written, $ TargetFile. On the home page of the website, when you want to call N, you want to generate a static web page when you call N, it is mightic to generate a static web page, which has greatly improved access speed, and also reduces the server burden. You can see that I use OB_ just to handle a single page, which is not possible for batch writing or updating multiple pages. This is the third method I want to talk, with template. The template is a good stuff. Now everyone is more or less in use. It is recommended not to simply handle netizens to handle it. It takes time to learn it, and the general template processing program is simple. The generation of a static web page with a template is very simple, the method is to obtain the analysis result and write the analysis results into the file. Below, you will talk about the template.inc in phplib. Generate a static web page with the template. First, modify Template.inc Add the following functions: // Save the analysis results to 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 that the result is saved to the static file, the second is to set all the template analysis variables to empty, so as to avoid batch processing influences. Second, realize static web generation.

set_file ("main", "mian.tpl"); // analysis template variable ....// Analysis Mainmains $ TPL-> Parse ("MAINS", "Main"); / / Put the analysis results into main.html $ TPL-> Savetofile ("main.html", "mains"); // Set your empty $ tpl-> rent (); // is important?> Oh, is it? Very simple, main.html is what we want. Below is an example in which a database is combined, and the function is encapsulated. Set_File ("main", $ template); // Put the {title} variable in the template Change to $ TITLE $ TPL-> Set_var ("Title", $ TITLE "); // Analyze the entire template $ TPL-> set_var (" mains "," main "); // Write MAINS to file $ TPL-> Savetofile ($ TARGET, "MAINS"); // Sets Air $ TPL-> Renew ();}?> This can use the function staticInfo () to generate a static web page in any article we want to handle. $ TARGET can also contain article content, author, source, etc., the method is the same. Third, update static webpage An article After adding the database, after some reason we always modify some articles. At this time, just put the corresponding static The web page is regenerated. This is very convenient because there is a static web page destination address Target field. It can be seen that the key to generate a static web page is $ template (template address), $ TARGET (target address). We can determine first, the latter can set an address for each article as you want. Commonly used 1, timestamp 2, time division 3, according to the article ID. Because these repetitions The opportunity will be very small. IV. Batch generation static web page. With a function of a single article, the function is very simple. It is very simple to get all the article AID, then set the function.

转载请注明原文地址:https://www.9cbs.com/read-68793.html

New Post(0)