浅谈PHP生成静态页的两种方法
发布于 2006-12-27 13:31 5 阅读:12,508 评论:5 标签: php 方法 静态

最近作的一个项目中用到了两种用PHP生成静态页面的方法,回想起当初自己还不知道如何生成静态页面的迷惘,以及看不懂高手写的文章的痛苦,觉得自己有必要站出来为还不知道如何生成静态页的phper写一个通俗点文章,以帮助他们尽快掌握这个好东西。

在我之前所见的文章中要不是用代码堆砌空间就是用高手与高手交流用的语言让新人望而生却。因此本文尽量把整体思路说得详尽点。

两种方法简单说明如下:
1. 使用文件函数得到静态页面的模板字符串,然后用str_replace函数将需要替换的东西替换了再写入到新的文件中。
2. 利用PHP的输出控制函数(Output Control)得到静态页面字符串,再写入到新的文件中。

下面开始详细的说明。

一. 利用模板生成

什么是模板?如果大家使用过Dreamwerver中的“另存为模板”就应该知道模板是用来统一风格的东西。它只让你修改页面的某一部分,当然这“某一部分”是由你来确定的。本文在这说的模板也就是这个意思。(此外,PHP模板技术还包括phplib、smarty等等,这不是本文所说内容了)

把模板的概念结合本文再说得具体一点就是:美工先做好一个页面,然后我们把这个页面当作模板(要注意的是这个模板就没必要使用EditRegion3这样的代码了,这种代码是Dreamwerver为了方便自己设计而弄的标识),把这个模板中我们需要改变的地方用一个与HTML可以区分的字符代替,如“{title}”、“[title]”。在生成静态页面的时候只需要把数据和这些字符串替换即可。这就是模板的含义了。

下面来说一下具体的实现思路:做一个模板

不错

不错,呵呵,我加到我的网里了
这个网,就是利用ob_start()
http://ycxx.512j.com

by 墨斗鱼 2007-01-16 15:40:51
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
文章不错啊

不错的文章

by 发芽 2006-12-30 09:01:24
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
44

这个页面真难看

by 44 2006-12-30 08:50:19
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
速度真快啊

真是光速啊,这么快就要结婚了

by 芽雨 2006-12-27 21:40:45
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172
发表了啊

我那天看了一遍,后来忙着追户口领结婚证就没来得及再看。结果盗现在单位还没给弄好户口,郁闷ing

by 赵序庚 2006-12-27 18:50:41
Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 154

Warning: Trying to access array offset on value of type bool in /opt/bitnami/apache/htdocs/workingsmarty/templates_c/e21e83752348f01f75c472238af0e4512d51cc32_0.file.left_look.html.php on line 172