php网页静态化及模板原理演示

看了一些开源系统的,简单的总结一下php的模板及静态原理。
先贴代码,再做解释。
index.php

<?php
//如果已存在静态页面,直接读取并显示
if(file_exists('index.html'))
{
echo file_get_contents('index.html');
}
else
{
//这里把需要的变量都附好值
$var = "Hello,World.";
//开启输出缓存
ob_start();
//这里调用模板,模板里嵌入一些PHP标签,用来显示变量的值
require_once('template.php');
//这里得到输出缓存,也就是调用模板后,将来要显示到页面上的内容
$out = ob_get_contents();
//把要显示的内容保存成一个文件
file_put_contents('index.html',$out);
//输出
ob_end_flush();
}

template.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>HTML</title>
</head>
<body>
<hr/>
<p>
<?php echo $var;?>
</p>
<hr/>
</body>
</html>

代码解释见注释。

续:[url]http://baiyuxiong.iteye.com/blog/796644[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值