Pain 全世界最小最简单的PHP模板引擎 (普通版)
前端之家收集整理的这篇文章主要介绍了
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.