微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

打包下载Pain.PHP
<div class="codetitle"><a style="CURSOR: pointer" data="69491" class="copybut" id="copybut69491" onclick="doCopy('code69491')"> 代码如下:

<div class="codebody" id="code69491">
<?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
<div class="codetitle"><a style="CURSOR: pointer" data="61205" class="copybut" id="copybut61205" onclick="doCopy('code61205')"> 代码如下:
<div class="codebody" id="code61205">
<?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
<div class="codetitle"><a style="CURSOR: pointer" data="39178" class="copybut" id="copybut39178" onclick="doCopy('code39178')"> 代码如下:
<div class="codebody" id="code39178">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

@H_502_61@
new_file


{@$songyu@}

{@$zhangyuan@}


版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

模板引擎

相关推荐