网络编程 
首页 > 网络编程 > 浏览文章

Pain 全世界最小最简单的PHP模板引擎 (普通版)

(编辑:jimmy 日期: 2024/5/9 浏览:3 次 )
打包下载

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 学习提高路线分享
一句话新闻
高通与谷歌联手!首款骁龙PC优化Chrome浏览器发布
高通和谷歌日前宣布,推出首次面向搭载骁龙的Windows PC的优化版Chrome浏览器。
在对骁龙X Elite参考设计的初步测试中,全新的Chrome浏览器在Speedometer 2.1基准测试中实现了显著的性能提升。
预计在2024年年中之前,搭载骁龙X Elite计算平台的PC将面世。该浏览器的提前问世,有助于骁龙PC问世就获得满血表现。
谷歌高级副总裁Hiroshi Lockheimer表示,此次与高通的合作将有助于确保Chrome用户在当前ARM兼容的PC上获得最佳的浏览体验。
友情链接:杰晶网络 DDR爱好者之家 南强小屋 黑松山资源网 白云城资源网