Pain 全世界最小最简单的PHP模板引擎 (普通版)


Posted in PHP onOctober 23, 2011

打包下载

Pain.php

<?php 
class Pain 
{ 
public $var=array(); 
public $tpl=array(); 
//this is the method to assign vars to the template 
public function assign($variable,$value=null) 
{ 
$this->var[$variable]=$value; 
} 
public function display($template_name,$return_string=false) 
{ 
//first find whether the tmp file in tmp dir exists. 
if(file_exists("tmp/temp_file.php")) 
{ 
unlink("tmp/temp_file.php"); 
} 
extract($this->var); 
$tpl_content=file_get_contents($template_name); 
$tpl_content=str_replace("{@", "<?php echo ", $tpl_content); 
$tpl_content=str_replace("@}", " ?>", $tpl_content); 
//create a file in the /tmp dir and put the $tpl_contentn into it, then 
//use 'include' method to load it! 
$tmp_file_name="temp_file.php"; 
//$tmp is the handler 
$tmp=fopen("tmp/".$tmp_file_name, "w"); 
fwrite($tmp, $tpl_content); 
include "tmp/".$tmp_file_name; 
} 
} 
?>

test.php
<?php 
require_once "Pain.php"; 
$pain=new Pain(); 
$songyu="songyu nb"; 
$zhangyuan="zhangyuan sb"; 
$pain->assign("songyu",$songyu); 
$pain->assign("zhangyuan",$zhangyuan); 
$pain->display("new_file.html"); 
?>

new_file.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" 
"http://www.w3.org/TR/html4/strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>new_file</title> 
</head> 
<body> 
{@$songyu@}<br/> 
{@$zhangyuan@} 
</body> 
</html>
PHP 相关文章推荐
把PHP安装为Apache DSO
Oct 09 PHP
如何过滤高亮显示非法字符
Oct 09 PHP
smarty静态实验表明,网络上是错的~呵呵
Nov 25 PHP
php 数组的创建、调用和更新实现代码
Mar 09 PHP
重新封装zend_soap实现http连接安全认证的php代码
Jan 12 PHP
浅析PHP微信支付通知的处理方式
May 25 PHP
PHP实现对文本数据库的常用操作方法实例演示
Jul 04 PHP
微信公众平台实现获取用户OpenID的方法
Apr 15 PHP
使用URL传输SESSION信息
Jul 14 PHP
PHP设置头信息及取得返回头信息的方法
Jan 25 PHP
PHP微商城开源代码实例
Mar 27 PHP
Laravel 修改默认日志文件名称和位置的例子
Oct 17 PHP
供参考的 php 学习提高路线分享
Oct 23 #PHP
PHP中的strtr函数使用介绍(str_replace)
Oct 20 #PHP
PHP中读写文件实现代码
Oct 20 #PHP
Array of country list in PHP with Zend Framework
Oct 17 #PHP
php环境配置之CGI、FastCGI、PHP-CGI、PHP-FPM、Spawn-FCGI比较?
Oct 17 #PHP
jQuery EasyUI API 中文文档 - DateBox日期框
Oct 15 #PHP
30 个很棒的PHP开源CMS内容管理系统小结
Oct 14 #PHP
You might like
php中serialize序列化与json性能测试的示例分析
2013/04/27 PHP
ThinkPHP3.1新特性之动态设置自动完成及自动验证示例代码
2014/06/23 PHP
PHP实现的多文件上传类及用法示例
2016/05/06 PHP
php中yar框架实例用法讲解
2020/12/27 PHP
js判断IE6/IE7/FF的代码[XMLHttpRequest]
2011/02/16 Javascript
基于JavaScript自定义构造函数的详解说明
2013/04/24 Javascript
javascript-简单的日历实现及Date对象语法介绍(附图)
2013/05/30 Javascript
理解JavaScript中Promise的使用
2016/01/18 Javascript
JS字符串的切分用法实例
2016/02/22 Javascript
关于JS中的apply,call,bind的深入解析
2016/04/05 Javascript
Vue.js学习笔记之常用模板语法详解
2017/07/25 Javascript
javascript中神奇的 Date对象小结
2017/10/12 Javascript
JS代码实现电脑配置检测功能
2018/03/21 Javascript
Vue利用canvas实现移动端手写板的方法
2018/05/03 Javascript
Vue2.2.0+新特性整理及注意事项
2018/08/22 Javascript
Vue2 监听属性改变watch的实例代码
2018/08/27 Javascript
Python中使用pprint函数进行格式化输出的教程
2015/04/07 Python
详解flask入门模板引擎
2018/07/18 Python
python创建文件备份的脚本
2018/09/11 Python
详解python之heapq模块及排序操作
2019/04/04 Python
Python 通过打码平台实现验证码的实现
2019/05/13 Python
Python 的字典(Dict)是如何存储的
2019/07/05 Python
学习Python爬虫的几点建议
2020/08/05 Python
利用CSS3的定位页面元素
2009/08/29 HTML / CSS
HTML5 虚拟键盘出现挡住输入框的解决办法
2017/02/14 HTML / CSS
微信小程序之html5 canvas绘图并保存到系统相册
2019/06/20 HTML / CSS
美国休闲服装品牌:J.Crew Factory
2017/03/04 全球购物
介绍一下gcc特性
2015/10/31 面试题
高中打架检讨书
2014/02/13 职场文书
教师读书活动总结
2014/05/07 职场文书
党员领导干部承诺书
2014/05/28 职场文书
党委班子剖析材料
2014/08/21 职场文书
领导干部“四风”问题批评与自我批评材料
2014/09/24 职场文书
乡镇2014法制宣传日活动总结
2014/11/01 职场文书
2015年教师师德师风承诺书
2015/04/28 职场文书
CSS变量实现主题切换的方法
2021/06/23 HTML / CSS