Home >Backend Development >PHP Tutorial >Use libtemplate to generate static web pages_PHP tutorial
by coldwind/iwind/month/ccterran http://iwind.org
For a long time, I asked others how to implement static web pages, and also asked questions to Nagging. Now, I finally came up with a very good idea. The simple way is to use libtemplate to implement it.
Everyone, take a look: http://doc.iwind.org
Now let me talk about how to implement static web page output.
1. Modify libtemplate.
Add two 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);
}
The dir in the function is where we want to save the file. varname is $target in libtemplate, used as follows:
$tpl->set_file("main","main.tpl");
...
$tpl->parse("mains ","main");
$tpl->savetofile("html/main.html","mains");
I believe readers who understand libtemplate can easily understand these.
//Cleared Assignment array
function renew(){
$this->varkeys=array();
$this->varvals=array();
$this->file=array( ; ;set_file("main","main.tpl");
...
$tpl->parse("mains","main");
$tpl->savetofile(" html/main.html","mains");
But once we change the template for article display, how can we quickly update the previously generated static web page? Obviously we need to loop the output. The example is as follows:
html_info(){//Single file, just a similar example
global $tpl;
$tpl->set_file("main","main.tpl");
...
$tpl->parse("mains","main");
$tpl->savetofile("html/main.html","mains");
$tpl->renew (); // Crucial
}
//Loop output
for($i=0;$i<$total;$i++){
}
So it is very simple to generate static pages. Updating is not as complicated as imagined.
(End)
http://www.bkjia.com/PHPjc/315097.html
www.bkjia.com
true