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+dbfile开发小型留言本
Oct 09 PHP
PHP COOKIE设置为浏览器进程
Jun 21 PHP
数据库中排序的对比及使用条件详解
Feb 23 PHP
$_GET['goods_id']+0 的使用详解
Jun 06 PHP
web server使用php生成web页面的三种方法总结
Oct 28 PHP
php判断GIF图片是否为动画的方法
Sep 04 PHP
PHP日期函数date格式化UNIX时间的方法
Mar 19 PHP
PHP输出一个等腰三角形的方法
May 12 PHP
在Thinkphp中使用ajax实现无刷新分页的方法
Oct 25 PHP
详解PHP神奇又有用的Trait
Mar 25 PHP
php实现的简单多进程服务器类完整示例
Feb 01 PHP
PHP正则之正向预查与反向预查讲解与实例
Apr 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
PHP个人网站架设连环讲(三)
2006/10/09 PHP
PHP中计算字符串相似度的函数代码
2012/12/29 PHP
PHP读取目录树的实现方法分析
2019/03/22 PHP
JS验证日期的格式YYYY-mm-dd 具体实现
2013/06/29 Javascript
向左滚动文字 js代码效果
2013/08/17 Javascript
toggle一个div显示或隐藏且可扩展成自定义下拉框
2013/09/12 Javascript
Javascript动态创建div的方法
2015/02/09 Javascript
轻松实现javascript数据双向绑定
2015/11/11 Javascript
JavaScript提高性能知识点汇总
2016/01/15 Javascript
详解Javacript和AngularJS中的Promises
2016/02/09 Javascript
三个js循环的关键字示例(for与while)
2016/02/16 Javascript
NodeJS创建基础应用并应用模板引擎
2016/04/12 NodeJs
微信小程序之仿微信漂流瓶实例
2016/12/09 Javascript
JavaScript回调函数callback用法解析
2020/01/14 Javascript
如何配置vue.config.js 处理static文件夹下的静态文件
2020/06/19 Javascript
浅析Python3爬虫登录模拟
2018/02/07 Python
解决django前后端分离csrf验证的问题
2019/02/03 Python
Python3中的bytes和str类型详解
2019/05/02 Python
python程序快速缩进多行代码方法总结
2019/06/23 Python
Django CBV类的用法详解
2019/07/26 Python
python3实现mysql导出excel的方法
2019/07/31 Python
django框架中间件原理与用法详解
2019/12/10 Python
Python加密模块的hashlib,hmac模块使用解析
2020/01/02 Python
pyecharts绘制中国2020肺炎疫情地图的实例代码
2020/02/12 Python
Python 窗体(tkinter)下拉列表框(Combobox)实例
2020/03/04 Python
pygame实现飞机大战
2020/03/11 Python
使用pandas实现筛选出指定列值所对应的行
2020/12/13 Python
Python爬虫新手入门之初学lxml库
2020/12/20 Python
FOREO官方网站:LUNA露娜洁面仪
2016/11/28 全球购物
巴西女装购物网站:Eclectic
2018/04/24 全球购物
法国足球商店:Footcenter
2019/07/06 全球购物
善意的谎言事例
2014/02/15 职场文书
节能减耗标语
2014/06/21 职场文书
护理实习生带教计划
2015/01/16 职场文书
南极大冒险观后感
2015/06/05 职场文书
关于实现中国梦的心得体会
2016/01/05 职场文书