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

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

template.php

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>HTML</title>
  6. </head>
  7. <body>
  8. <hr/>
  9. <p>
  10. <?php echo $var;?>
  11. </p>
  12. <hr/>
  13. </body>
  14. </html>

代码解释见注释。

转自http://baiyuxiong.iteye.com/blog/796644

最新文章

  1. Spring Boot with Spring Data JPA (1) - Concept
  2. 如何在 IDEA 中使用Maven 及 相应插件
  3. Android GPS定位 获取经纬度
  4. POJ 1185 经典状压dp
  5. thymeleaf 局部变量、属性优先级、注释
  6. 我和Cpp的第一次正式约会
  7. 推送:腾迅信鸽 VS Bmob
  8. 关于ueditor1.4.2 与Jquery 验证同时使用失效
  9. 1639: [Usaco2007 Mar]Monthly Expense 月度开支
  10. 201521123121 《Java程序设计》第5周学习总结
  11. 如何将Provisioning Profile安装到开发的Mac系统上
  12. MVC最全jar包
  13. CentOS安装、配置Nginx反向代理
  14. 1873 初中的算术(java大数)
  15. python之input()、while、title()和upper()
  16. linux-nohup后台运行
  17. c# url链接转成二维码图片,再转成byte[]二进制流,输出到前段ajax
  18. apache允许列目录
  19. x64位windows 2003 server中“Server 对象 错误 &#39;ASP 0177 : 800700c1&#39; Server.CreateObject 失败”问题
  20. 洛谷P1832 A+B Problem(再升级)

热门文章

  1. JavaScript图片轮播,举一反三
  2. 12 Python+selenium对日期控件进行处理(采用执行JS脚本)
  3. We wanted {&quot;required&quot;:[&quot;value&quot;]} and you sent [&quot;text&quot;,&quot;value&quot;,&quot;id&quot;,&quot;sessionId&quot;]
  4. react-draft-wysiwyg富文本
  5. Jenkins构建项目
  6. python tips:类的专有属性
  7. day36 类的三大特性---封装以及Property特性
  8. hibernate与spring整合
  9. luogu P4719 【模板】动态 DP 矩阵乘法 + LCT
  10. wafII笔记