I also came to an original article--using Libtemplate to achieve static Web page generation

Source: Internet
Author: User
static | Web Page | original by Coldwind/iwind/month/ccterran http://iwind.org
For a long time I asked people everywhere how to implement static Web pages, but also to nagging questions, and now, I finally came up with a very simple way, that is to use libtemplate to achieve it.
Everyone look: http://doc.iwind.org
I'll talk about how to implement the static page output below.
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 the libtemplate $target, using the following:
$TPL->set_file ("main", "Main.tpl");
...
$tpl->parse ("mains", "main");
$tpl->savetofile ("html/main.html", "mains");
It is easy to understand the readers who understand libtemplate.
Clearing an array of assigned values
function Renew () {
$this->varkeys=array ();
$this->varvals=array ();
$this->file=array ();
}
This clears the impact of the previous template analysis.
2, to achieve static output.
If the output is a single file as an example above.
$TPL->set_file ("main", "Main.tpl");
...
$tpl->parse ("mains", "main");
$tpl->savetofile ("html/main.html", "mains");

But once we change the template that the article shows, how do you make the static Web pages that were generated so quickly? It's obvious to recycle the output. Examples are 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 ()/Critical
}
Loop output
for ($i =0; $i < $total; $i + +) {
...
Html_info;
}
So it's simple to implement a static page. The update is not as complicated as it might seem.
Finish

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.