Pain 全世界最小最简单的PHP模板引擎 (普通版)
时间:2023-11-18 09:12:06
打包下载
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>
标签:模板引擎
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
如何使用python获取现在的日期与时间
2021-07-21 16:50:12
![](https://img.aspxhome.com/file/2023/7/120567_0s.jpg)
Python计算两个矩形重合面积代码实例
2021-12-18 23:34:22
matplotlib 输出保存指定尺寸的图片方法
2021-09-06 11:29:12
python中pip的使用和修改下载源的方法
2023-08-04 21:33:41
python中的Numpy二维数组遍历与二维数组切片后遍历效率比较
2022-11-23 04:47:50
ASP.NET Core Authentication认证实现方法
2023-07-21 12:21:31
![](https://img.aspxhome.com/file/2023/8/78078_0s.jpg)
python数据分析近年比特币价格涨幅趋势分布
2022-04-02 15:05:56
![](https://img.aspxhome.com/file/2023/1/81591_0s.png)
MSSQL中递归SQL查询语句实例说明-
2011-09-30 11:42:43
解决Python logging模块无法正常输出日志的问题
2023-10-03 17:04:25
Bootstrap fileinput 上传新文件移除时触发服务器同步删除的配置
2024-04-19 09:45:02
![](https://img.aspxhome.com/file/2023/9/136399_0s.png)
解决mysql5中文乱码问题的方法
2024-01-15 07:47:52
通过 for 循环比较 Python 与 Ruby 的编程区别
2022-11-12 01:19:26
如何查询Top N及Top(M―N)记录?
2009-11-11 20:03:00
对python操作kafka写入json数据的简单demo分享
2023-05-04 21:24:08
Python Pygame实战之飞机大战的实现
2023-10-19 17:30:30
![](https://img.aspxhome.com/file/2023/7/97107_0s.jpg)
Python reshape的用法及多个二维数组合并为三维数组的实例
2021-12-18 10:29:25
![](https://img.aspxhome.com/file/2023/4/102324_0s.jpg)
python通过get,post方式发送http请求和接收http响应的方法
2022-04-09 04:37:54
pycharm2021激活码使用教程(永久激活亲测可用)
2022-02-05 05:02:17
![](https://img.aspxhome.com/file/2023/9/66829_0s.png)
OpenCV Python实现拼图小游戏
2021-07-30 18:10:34
![](https://img.aspxhome.com/file/2023/2/79762_0s.gif)
PyInstaller如何打包依赖文件至目标程序目录
2021-05-31 12:08:12
![](https://img.aspxhome.com/file/2023/7/130817_0s.jpg)