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 第二节 数据类型之数组
Apr 28 PHP
教你如何在CI框架中使用 .htaccess 隐藏url中index.php
Jun 09 PHP
PHP原生函数一定好吗?
Dec 08 PHP
php数组合并与拆分实例分析
Jun 12 PHP
php强制用户转向www域名的方法
Jun 19 PHP
php将一维数组转换为每3个连续值组成的二维数组
May 06 PHP
PHP 的比较运算与逻辑运算详解
May 12 PHP
PHP实现批量检测网站是否能够正常打开的方法
Aug 23 PHP
PHP制作登录异常ip检测功能的实例代码
Nov 16 PHP
Yii1.1中通过Sql查询进行的分页操作方法
Mar 16 PHP
彻底搞懂PHP 变量结构体
Oct 11 PHP
PHP的mysqli_stmt_init()函数讲解
Jan 24 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版本实现代码
2012/09/15 PHP
PHP中使用FFMPEG获取视频缩略图和视频总时长实例
2014/05/04 PHP
php获取数组元素中头一个数组元素值的实现方法
2014/12/20 PHP
jquery 新浪网易的评论块制作
2010/07/01 Javascript
Javascript中产生固定结果的函数优化技巧
2013/01/16 Javascript
动态加载iframe时get请求传递中文参数乱码解决方法
2014/05/07 Javascript
微信公众号开发 自定义菜单跳转页面并获取用户信息实例详解
2016/12/08 Javascript
详解Vue 事件修饰符capture 的使用
2017/12/29 Javascript
详解Vue快速零配置的打包工具——parcel
2018/01/16 Javascript
BootStrap自定义popover,点击区域隐藏功能的实现
2018/01/23 Javascript
使用Python脚本将文字转换为图片的实例分享
2015/08/29 Python
Python数据可视化编程通过Matplotlib创建散点图代码示例
2017/12/09 Python
Django进阶之CSRF的解决
2018/08/01 Python
Python OpenCV对本地视频文件进行分帧保存的实例
2019/01/08 Python
PyQt5实现简单数据标注工具
2019/03/18 Python
PyCharm安装Markdown插件的两种方法
2019/06/24 Python
python3.x+pyqt5实现主窗口状态栏里(嵌入)显示进度条功能
2019/07/04 Python
python对Excel按条件进行内容补充(推荐)
2019/11/24 Python
tensorflow之获取tensor的shape作为max_pool的ksize实例
2020/01/04 Python
pytorch动态网络以及权重共享实例
2020/01/06 Python
使用 tf.nn.dynamic_rnn 展开时间维度方式
2020/01/21 Python
Python 之 Json序列化嵌套类方式
2020/02/27 Python
keras实现theano和tensorflow训练的模型相互转换
2020/06/19 Python
Python 执行矩阵与线性代数运算
2020/08/01 Python
canvas 橡皮筋式线条绘图应用方法
2019/02/13 HTML / CSS
家用个人磨皮机:Trophy Skin
2017/03/30 全球购物
美国销售第一的智能手机和平板电脑保护壳:OtterBox
2017/12/21 全球购物
英国最大的LED专业零售商:Led Hut
2018/03/16 全球购物
Janie and Jack美国官网:GAP旗下的高档童装品牌
2019/09/09 全球购物
函授自我鉴定
2013/11/06 职场文书
化工专业推荐信范文
2013/11/28 职场文书
中式面点餐厅创业计划书
2014/01/29 职场文书
《蚂蚁和蝈蝈》教学反思
2014/02/24 职场文书
好的促销活动方案
2014/08/21 职场文书
2019年怎样写好导游词?
2019/07/02 职场文书
读《庄子》有感:美而不自知
2019/11/06 职场文书