Using Libtemplate to implement static Web page generation _php

Source: Internet
Author: User
by Coldwind/iwind/month/ccterran http://iwind.org
For a long time I have been asking people 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.
See all: http://doc.iwind.org
Let's talk about how to implement static Web page output.
1, modify the libtemplate.
Add two 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);
}
The dir in the function is where we want to save the file. VarName is the libtemplate in $target, using the following:
$TPL->set_file ("main", "Main.tpl");
...
$tpl->parse ("mains", "main");
$tpl->savetofile ("html/main.html", "mains");
It is easy for readers who understand Libtemplate to understand this.
Clears an array of values that have been assigned
function Renew () {
$this->varkeys=array ();
$this->varvals=array ();
$this->file=array ();
}
This clears the impact of the previous template analysis.
2, implement static output.
If the output is a single file as in the above example.
$TPL->set_file ("main", "Main.tpl");
...
$tpl->parse ("mains", "main");
$tpl->savetofile ("html/main.html", "mains");

But once we have changed the template shown in the article, how can we make the previously generated static Web page update quickly? It is clear that you want to loop 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 very 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.