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 Ajax乱码
Apr 09 PHP
php实现mysql数据库操作类分享
Feb 14 PHP
PHP开发微信支付的代码分享
May 25 PHP
PHP四舍五入、取整、round函数使用示例
Feb 06 PHP
linux下实现定时执行php脚本
Feb 13 PHP
使用PHP实现生成HTML静态页面
Nov 18 PHP
php结合web uploader插件实现分片上传文件
May 10 PHP
Windows下php+mysql5.7配置教程
May 16 PHP
PHP简单实现模拟登陆功能示例
Sep 15 PHP
php单元测试phpunit入门实例教程
Nov 17 PHP
php工具型代码之印章抠图
Jul 18 PHP
php学习笔记之字符串常见操作总结
Jul 16 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与XML的PDF文档生成技术
2006/10/09 PHP
php自定义apk安装包实例
2014/10/20 PHP
php正则匹配文章中的远程图片地址并下载图片至本地
2015/09/29 PHP
PHP按指定键值对二维数组进行排序的方法
2015/12/22 PHP
textContent在Firefox下与innerText等效的属性
2007/05/12 Javascript
使用基于jquery的gamequery插件做JS乒乓球游戏
2011/07/31 Javascript
jquery实现的鼠标拖动排序Li或Table
2014/05/04 Javascript
Javascript中封装window.open解决不兼容问题
2014/09/28 Javascript
Node调用Java的示例代码
2017/09/20 Javascript
webstrom Debug 调试vue项目的方法步骤
2018/07/17 Javascript
微信小程序中使用wxss加载图片并实现动画效果
2018/08/13 Javascript
layui弹出层按钮提交iframe表单的方法
2018/08/20 Javascript
浅谈Vue页面级缓存解决方案feb-alive(上)
2019/04/14 Javascript
微信小程序地图导航功能实现完整源代码附效果图(推荐)
2019/04/28 Javascript
JavaScript 链表定义与使用方法示例
2020/04/28 Javascript
python查找第k小元素代码分享
2013/12/18 Python
视觉直观感受若干常用排序算法
2017/04/13 Python
Python 中的Selenium异常处理实例代码
2018/05/03 Python
pandas将numpy数组写入到csv的实例
2018/07/04 Python
python3转换code128条形码的方法
2019/04/17 Python
Django基础知识 URL路由系统详解
2019/07/18 Python
基于python框架Scrapy爬取自己的博客内容过程详解
2019/08/05 Python
使用PyWeChatSpy自动回复微信拍一拍功能的实现代码
2020/07/02 Python
使用jupyter notebook运行python和R的步骤
2020/08/13 Python
英国户外装备商店:Ultimate Outdoors
2019/05/07 全球购物
使用Vue.js和MJML创建响应式电子邮件
2021/03/23 Vue.js
小组合作学习反思
2014/02/18 职场文书
婚庆司仪主持词
2014/03/15 职场文书
施工工地安全标语
2014/06/07 职场文书
525心理活动总结
2014/07/04 职场文书
新员工入职欢迎词
2015/01/23 职场文书
小学班主任教育随笔
2015/08/15 职场文书
浅谈移动端中的视口(viewport)的具体使用
2021/04/13 HTML / CSS
FP-growth算法发现频繁项集——构建FP树
2021/06/24 Python
Python Django模型详解
2021/10/05 Python
SQL中去除重复数据的几种方法汇总(窗口函数对数据去重)
2023/05/08 MySQL