php实现页面纯静态

来源:互联网 发布:淘宝商品网页无法显示 编辑:程序博客网 时间:2024/06/16 03:15

1.先来看下面代码index.php

<?php// 准备要展示到网页的数据$data = array(     array('id'=>1,'msg'=>'hello java'),    array('id'=>2,'msg'=>'hello php'),    array('id'=>3,'msg'=>'hello python'),);// 渲染到模板// 实际项目一般是在html里渲染// 这里演示 希望能看懂foreach($data as $item){    echo $item['id'].'===>'.$item['msg'].'<br/>';}

我们可以想象访问index.php是什么一个页面效果,但是这个可不是我们想要的纯静态页面哦。

我们已经学过了php实现页面静态化的原理:
http://blog.csdn.net/github_26672553/article/details/72871744
下面来实现一下,看看需要改动哪些代码。

<?php// 准备要展示到网页的数据$data = array(     array('id'=>1,'msg'=>'hello java'),    array('id'=>2,'msg'=>'hello php'),    array('id'=>3,'msg'=>'hello python'),);// 渲染到模板// 实际项目一般是在html里渲染// 这里演示 希望能看懂ob_start(); // 开始输入缓冲控制foreach($data as $item){    echo $item['id'].'===>'.$item['msg'].'<br/>';}// 开始生成静态页面文件if(file_put_contents('index.html',ob_get_contents())){    echo 'success';}else{    echo 'error';}

执行之后,就会生个一个index.html文件了,这就是我们真正需要的静态页面。
index.html内容如下:

1===>hello java<br/>2===>hello php<br/>3===>hello python<br/>

然后我们在浏览器访问index.html和最初访问index.php显示的内容一样,但是区别是index.html是静态页面。

原创粉丝点击