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.MVC的模板标签系统(三)
Sep 05 PHP
自动分页的不完整解决方案
Jan 12 PHP
使用apache模块rewrite_module (转)
Feb 14 PHP
PHP中“简单工厂模式”实例代码讲解
Sep 04 PHP
PHP编程函数安全篇
Jan 08 PHP
分享php代码将360浏览器导出的favdb的sqlite数据库文件转换为html
Dec 09 PHP
php bootstrap实现简单登录
Mar 08 PHP
PHP如何使用Memcached
Apr 05 PHP
php获取服务器操作系统相关信息的方法
Oct 08 PHP
Yii框架页面渲染操作实例详解
Jul 19 PHP
Thinkphp集成抖音SDK的实现方法
Apr 28 PHP
PHP的垃圾回收机制代码实例讲解
Feb 27 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与paypal整合方法
2010/11/28 PHP
php设计模式 State (状态模式)
2011/06/26 PHP
php正则表达式学习笔记
2015/11/13 PHP
详解WordPress中提醒安装插件以及隐藏插件的功能实现
2015/12/25 PHP
微信支付扫码支付php版
2016/07/22 PHP
PHP的mysqli_stmt_init()函数讲解
2019/01/24 PHP
浅谈thinkphp的nginx配置,以及重写隐藏index.php入口文件方法
2019/10/12 PHP
laravel5.5安装jwt-auth 生成token令牌的示例
2019/10/24 PHP
JavaScript的public、private和privileged模式
2009/12/28 Javascript
Jquery图形报表插件 jqplot简介及参数详解
2012/10/10 Javascript
JavaScript高级程序设计(第3版)学习笔记5 js语句
2012/10/11 Javascript
阻止事件(取消浏览器对事件的默认行为并阻止其传播)
2013/11/03 Javascript
JavaScript的arguments对象应用示例
2014/09/15 Javascript
深入浅出分析javaScript中this用法
2015/05/09 Javascript
avalon js实现仿微博拖动图片排序
2015/08/14 Javascript
JS判断输入字符串长度实例代码(汉字算两个字符,字母数字算一个)
2016/08/02 Javascript
JS作用域闭包、预解释和this关键字综合实例解析
2016/12/16 Javascript
js实现贪吃蛇小游戏(容易理解)
2017/01/22 Javascript
jQuery阻止移动端遮罩层后页面滚动
2017/03/15 Javascript
Django中传递参数到URLconf的视图函数中的方法
2015/07/18 Python
最大K个数问题的Python版解法总结
2016/06/16 Python
新年福利来一波之Python轻松集齐五福(demo)
2020/01/20 Python
python解释器pycharm安装及环境变量配置教程图文详解
2020/02/26 Python
python爬虫请求头的使用
2020/12/01 Python
css3制作彩色边线3d立体按钮的示例(css3按钮)
2014/05/06 HTML / CSS
canvas实现按住鼠标移动绘制出轨迹的示例代码
2018/02/05 HTML / CSS
阿迪达斯意大利在线商店:adidas意大利
2016/09/19 全球购物
泰国时尚电商:POMELO Fashion
2020/03/11 全球购物
DOUGLAS波兰:在线销售香水和化妆品
2020/07/05 全球购物
经贸日语专业个人求职信
2013/12/13 职场文书
2014政务公开实施方案
2014/02/19 职场文书
软件部经理岗位职责范本
2014/02/25 职场文书
投标承诺书范本
2014/03/27 职场文书
财产公证书
2014/04/10 职场文书
借款协议书范本
2014/04/22 职场文书
美食节目策划方案
2014/05/31 职场文书