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代码
Feb 16 PHP
PHP中使用smarty生成静态文件的例子
Apr 24 PHP
PHP记录搜索引擎蜘蛛访问网站足迹的方法
Apr 15 PHP
PHP批量生成图片缩略图的方法
Jun 18 PHP
Smarty简单生成表单元素的方法示例
May 23 PHP
谈谈php对接芝麻信用踩的坑
Dec 01 PHP
PHP面相对象中的重载与重写
Feb 13 PHP
php中pcntl_fork创建子进程的方法实例
Mar 14 PHP
php利用array_search与array_column实现二维数组查找
Jul 08 PHP
thinkphp5.1框架模板赋值与变量输出示例
May 25 PHP
PhpStorm 2020.3:新增开箱即用的PHP 8属性(推荐)
Oct 30 PHP
php中array_fill函数的实例用法
Mar 02 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 求质素(素数) 的实现代码
2011/04/12 PHP
PHP调用API接口实现天气查询功能的示例
2017/09/21 PHP
Laravel多用户认证系统示例详解
2018/03/13 PHP
js 屏蔽鼠标右键脚本附破解方法
2009/12/03 Javascript
Prototype源码浅析 String部分(四)之补充
2012/01/16 Javascript
JS通过ajax动态读取xml文件内容的方法
2015/03/24 Javascript
详解JavaScript基本类型和引用类型
2015/12/09 Javascript
用JavaScript动态建立或增加CSS样式表的实现方法
2016/05/20 Javascript
Bootstrap和Angularjs配合自制弹框的实例代码
2016/08/24 Javascript
AngularJS实现标签页的两种方式
2016/09/05 Javascript
JavaScript实现类似拉勾网的鼠标移入移出效果
2016/10/27 Javascript
微信小程序 Button 组件详解及简单实例
2017/01/10 Javascript
node.js基于mongodb的搜索分页示例
2017/01/22 Javascript
JS实现unicode和UTF-8之间的互相转换互转
2017/07/05 Javascript
简单实现js放大镜效果
2017/07/24 Javascript
利用three.js画一个3D立体的正方体示例代码
2017/11/19 Javascript
Vue实现移动端左右滑动效果的方法
2018/11/27 Javascript
微信小程序实现的picker多级联动功能示例
2019/05/23 Javascript
一篇文章弄懂javascript中的执行栈与执行上下文
2019/08/09 Javascript
vue 在单页面应用里使用二级套嵌路由
2020/12/19 Vue.js
浅谈Python实现Apriori算法介绍
2017/12/20 Python
python支付宝支付示例详解
2019/08/22 Python
Python进程间通信multiprocess代码实例
2020/03/18 Python
pyinstaller打包成无控制台程序时运行出错(与popen冲突的解决方法)
2020/04/15 Python
三步解决python PermissionError: [WinError 5]拒绝访问的情况
2020/04/22 Python
PyTorch中的拷贝与就地操作详解
2020/12/09 Python
python批量提取图片信息并保存的实现
2021/02/05 Python
Pycharm制作搞怪弹窗的实现代码
2021/02/19 Python
男女时尚与复古风格在线购物:RoseGal(全球免费送货)
2017/07/19 全球购物
澳大利亚当地最大的时装生产商:Cue
2018/08/06 全球购物
MYSQL基础面试题
2012/05/13 面试题
警察群众路线对照检查材料思想汇报
2014/10/01 职场文书
教育教学读书笔记
2015/07/02 职场文书
文明礼仪主题班会
2015/08/13 职场文书
PHP中多字节字符串操作实例详解
2021/08/23 PHP
vue数据字典取键值项目的字典问题
2022/04/12 Vue.js