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中for循环语句的几种变型
Nov 26 PHP
php cc攻击代码与防范方法
Oct 18 PHP
php操作xml
Oct 27 PHP
ThinkPHP查询中的魔术方法简述
Jun 25 PHP
php截取中文字符串函数实例
Feb 23 PHP
浅谈PHP eval()函数定义和用法
Jun 21 PHP
php车辆违章查询数据示例
Oct 14 PHP
PHP实现获取第一个中文首字母并进行排序的方法
May 09 PHP
Yii2.0多文件上传实例说明
Jul 24 PHP
Yii2语言国际化自动配置详解
Aug 22 PHP
CentOS7系统搭建LAMP及更新PHP版本操作详解
Mar 26 PHP
PHP数组基本用法与知识点总结
Jun 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设计模式 State (状态模式)
2011/06/26 PHP
PHP与javascript实现变量交互的示例代码
2013/07/23 PHP
PHP-Java-Bridge使用笔记
2014/09/22 PHP
php使用escapeshellarg时中文被过滤的解决方法
2016/07/10 PHP
PHP7扩展开发之hello word实现方法详解
2018/01/15 PHP
Laravel Eloquent ORM 实现查询表中指定的字段
2019/10/17 PHP
多广告投放代码 推荐
2006/11/13 Javascript
(仅IE下有效)关于checkbox 三态
2007/05/12 Javascript
jQuery入门介绍之基础知识
2015/01/13 Javascript
Javascript编写俄罗斯方块思路及实例
2015/07/07 Javascript
Angular 4依赖注入学习教程之ValueProvider的使用(七)
2017/06/04 Javascript
简单实现js放大镜效果
2017/07/24 Javascript
pm2 部署 node的三种方法示例
2017/10/20 Javascript
微信小程序tabBar模板用法实例分析【附demo源码下载】
2017/11/28 Javascript
vue组件实现进度条效果
2018/06/06 Javascript
详解ES6 export default 和 import语句中的解构赋值
2019/05/28 Javascript
JS 封装父页面子页面交互接口的实例代码
2019/06/25 Javascript
微信小程序实现收货地址左滑删除
2020/11/18 Javascript
vue和iview实现Scroll 数据无限滚动功能
2019/10/31 Javascript
vue.config.js中配置Vue的路径别名的方法
2020/02/11 Javascript
keep-alive不能缓存多层级路由菜单问题解决
2020/03/10 Javascript
Element实现表格嵌套、多个表格共用一个表头的方法
2020/05/09 Javascript
three.js中多线程的使用及性能测试详解
2021/01/07 Javascript
Python检测网站链接是否已存在
2016/04/07 Python
python+selenium实现自动抢票功能实例代码
2018/11/23 Python
Django 全局的static和templates的使用详解
2019/07/19 Python
Python 实现向word(docx)中输出
2020/02/13 Python
python3格式化字符串 f-string的高级用法(推荐)
2020/03/04 Python
Pytorch学习之torch用法----比较操作(Comparison Ops)
2020/06/28 Python
Python reversed反转序列并生成可迭代对象
2020/10/22 Python
linux面试相关问题
2013/04/28 面试题
租房协议书样本
2014/08/20 职场文书
工作粗心大意检讨书
2014/09/18 职场文书
高中家长意见怎么写
2015/06/03 职场文书
大学生社区义工服务心得体会
2016/01/22 职场文书
Python趣味实战之手把手教你实现举牌小人生成器
2021/06/07 Python