Home  >  Article  >  Backend Development  >  Use libTemplate to generate static web pages_PHP tutorial

Use libTemplate to generate static web pages_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:09:14717browse

作者:iwind

原来在dev-club发表的一篇文章,将怎么用模板处理程序PHPlib 中的template.inc实现静态网页的生成,呵呵,居然被列入精华,并被多个网站转载,想来这是荣幸。其实网上这方面的东西很多了,我上此发布的所谓IAMS( iwind 文章管理系统),里面也有,有心人可以看一下。下面我只是简要在总结一次。

现在一般说生成静态网页的方法有三种,一个是配置服务器,大家可以到http://www.devarticles.com/c/b/PHP/ 去找找看,对于这个很多地方都有的。另外一个是用ob_函数控制输出。方法如下:先用ob_start();打开输出缓冲器,然后是对数据的分析,操作等等,跟着用ob_get_contents();获取缓冲区的内容,然后再写入文件。根据这个步骤,可以写出以下程序:
   ob_start();
//主体部分,数据操作,处理,输出等等。。。
require”global.php”;
mysql_connect(“localhost”,”root”,””);
…..
//获取缓冲区内容
$contents=ob_get_contents();
//如果不想输出任何东西,可以加上这句
ob_end_clean();
//写入目的文件
$fp=@fopen($targetFile,”w+”) or die(“打开文件时出错”);
fwrite($fp,$contents);
?>

这样就把这个动态页面的内容写入了静态页面,$targetFile.像有的网站首页内容很多,要调用n多个查询语句时,不妨定时生成静态网页,既大幅提高了访问速度,也减轻了服务器负担。

你可以看出来,我用ob_只是处理单个页面,对于批量写入或更新多个页面,这个方法就不行了。这就是我要讲的第三种方法,用模板。模板是个好东东,现在大家都或多或少在用它,建议还不会简单模板处理的网友,花点时间去学它,一般的模板处理程序都很简单的。用模板实现静态网页的生成是非常简单的,方法就是获取分析结果,把分析结果写入文件。下面就以PHPlib中的template.inc来谈谈如果用模板生成静态网页。

一, 修改template.inc
加入以下的几个函数:
//将分析结果保存到文件中去
  function savetofile ($dir,$varname){
   $data=$this->finish($this->get_var($varname));
   $fp=fopen($dir,"w+");
   fwrite($fp,$data);
  }
  //清除已赋值数组
   function renew(){
    $this->varkeys=array();
    $this->varvals=array();
    $this->file=array();
    }

第一个函数是结果保存到静态文件中,第二个是把所有的模板分析变量都置为空,以免批量处理时相互影响。

二,实现静态网页生成。
$itpl->set_file(“main”,”mian.tpl”);
//分析模板变量
…..
//分析mainmains
$tpl->parse("mains","main");
//把分析结果mains存入main.html
$tpl->savetofile("main.html","mains");
//置空
$tpl->renew();//至关重要
?>

Haha, isn’t it very simple? main.html is the content we want. The following is an example of combining a database and encapsulating it with a function.
//$aid is the article id in the database, $table is the table name, $template is the template address, $tpl is an instance of template.inc
//Each aid Corresponding to a static web page address, there is a data table
//The structure of the table is similar to aid        ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​// 3 $table where aid='$aid'”);
//Fetch data
$array=mysql_fetch_array($res);
//Read static web page address, title.
$target=$array[“target”];
$title=$array[“title”];
//Analysis template
$tpl->set_file(“main”,$ template);
//Replace the {title} variable in the template with $title
$itpl->set_var(“title”,$title”);
//Analyze the entire template
$itpl->set_var("mains","main");
//Write mains to the file
$tpl->savetofile($target,"mains");
//Set Empty
$tpl->renew();
}
?>

This way we can use the function staticInfo() to generate any article we want to process Static web pages. Table $target can also contain article content, author, source, etc. The method is the same.

Third, update the static web page
After an article is added to the database, we always add it to the database for some reasons. You need to modify some articles. At this time, you only need to regenerate the corresponding static web page. This is very convenient, because the target address field of the static web page is already in the table. You can see the key. The key to generating a static web page for an article is $template (template address) and $target (target address). We can determine the former first, and the latter can be a commonly used address for each article. There are 1, timestamp 2, hours minutes and seconds 3, because the chance of these repetitions will be very small.

Four, static web pages are generated in batches.

With static web pages generated from a single article. Function, then batch generation is very simple. Just get all the article aid, and then insert the function.

//Introducing functions

require "functions.php";

//Definition of some variables
$table="art";

$template="template/info.tpl";

$tpl=new Template(".");

//Connect to 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 pages
staticInfo($aid);
}
//End
echo "All static web pages have been updated/generated successfully";
? >

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

Then, the website content we access will be static. One question is, will this method take up a lot of space? http://www.knowsky.com has thousands of articles and only takes up 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. In fact, the method is the same, which is to calculate the page number. → Analyze the content of each page number → Write to file. To analyze the content of each page number, of course you need to write a function. If you generate it page by page, I'm afraid you will be laughed at ^_^.

Static web pages can not only reduce the load on the server and improve the access speed, but can also be used as a mirror website for easy backup, reducing the loss caused by attacks and speeding up the restoration speed. Of course, static web pages will also bring a lot of inconvenience to everyone. You need to make a balance between dynamic and static. You can also add php code called by js to the static web pages to achieve counting, instant updates, etc. (End)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314562.htmlTechArticleAuthor: iwind An article originally published in dev-club, how to use template in the template processing program PHPlib .inc realizes the generation of static web pages, haha, it is actually included in the essence and is used by many...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn