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 mssql 日期出现中文字符的解决方法
Mar 10 PHP
php模拟socket一次连接,多次发送数据的实现代码
Jul 26 PHP
php循环语句 for()与foreach()用法区别介绍
Sep 05 PHP
php中is_null,empty,isset,unset 的区别详细介绍
Apr 28 PHP
setcookie中Cannot modify header information-headers already sent by错误的解决方法详解
May 08 PHP
ThinkPHP实现多数据库连接的解决方法
Jul 01 PHP
PHP图片处理之使用imagecopy函数添加图片水印实例
Nov 19 PHP
php等比例缩放图片及剪切图片代码分享
Feb 13 PHP
浅谈PHP Cookie处理函数
Jun 10 PHP
详解Yii实现分页的两种方法
Jan 14 PHP
thinkphp查询,3.X 5.0方法(亲试可行)
Jun 17 PHP
一次因composer错误使用引发的问题与解决
Mar 06 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
IIS6.0+PHP5.x+MySQL5.x+Zend3.0x+GD+phpMyAdmin2.8x通用安装实例(已经完成)
2006/12/06 PHP
php支付宝接口用法分析
2015/01/04 PHP
php实现根据词频生成tag云的方法
2015/04/17 PHP
UPUPW 更新 64 位 Apache 系列 PHP 7.0 正式版
2015/12/08 PHP
PHP汉字转换拼音的函数代码
2015/12/30 PHP
php+mongodb判断坐标是否在指定多边形区域内的实例
2016/10/28 PHP
PHP中使用jQuery+Ajax实现分页查询多功能操作(示例讲解)
2017/09/17 PHP
JavaScript入门教程(8) Location地址对象
2009/01/31 Javascript
jQuery对象和DOM对象使用说明
2010/06/25 Javascript
jquery判断checkbox(复选框)是否被选中的代码
2010/10/20 Javascript
js中for in的用法示例解析
2013/12/25 Javascript
扩展jQuery对象时如何扩展成员变量具体怎么实现
2014/04/25 Javascript
js中hash和ico的关联分析
2015/02/05 Javascript
JS中改变this指向的方法(call和apply、bind)
2016/03/26 Javascript
JS中多步骤多分步的StepJump组件实例详解
2016/04/01 Javascript
jQuery ajax全局函数处理session过期后的ajax跳转问题
2016/06/03 Javascript
解决wx.onMenuShareTimeline出现的问题
2016/08/16 Javascript
第一次记录Bootstrap table学习笔记(1)
2017/05/18 Javascript
vue2.0$nextTick监听数据渲染完成之后的回调函数方法
2018/09/11 Javascript
创建Vue项目以及引入Iview的方法示例
2018/12/03 Javascript
windows下create-react-app 升级至3.3.1版本踩坑记
2020/02/17 Javascript
JavaScript canvas绘制折线图
2020/02/18 Javascript
vue使用video插件vue-video-player详解
2020/10/23 Javascript
[01:13:08]2018DOTA2亚洲邀请赛4.6 淘汰赛 mineski vs LGD 第二场
2018/04/10 DOTA
Python获取DLL和EXE文件版本号的方法
2015/03/10 Python
Python实现多进程的四种方式
2019/02/22 Python
python全栈要学什么 python全栈学习路线
2019/06/28 Python
关于keras.layers.Conv1D的kernel_size参数使用介绍
2020/05/22 Python
Cult Gaia官网:美国生活方式品牌
2019/08/16 全球购物
员工评语大全
2014/01/19 职场文书
电大毕业自我鉴定
2014/02/03 职场文书
灵山大佛导游词
2015/02/04 职场文书
免职通知
2015/04/23 职场文书
Python 如何将integer转化为罗马数(3999以内)
2021/06/05 Python
winserver2019安装软件一直卡在应用程序正在为首次使用做准备
2022/06/10 Servers
深入理解pytorch库的dockerfile
2022/06/10 Python