Pain 全世界最小最简单的PHP模板引擎 (普通版)_PHP教程
程序员文章站
2022-05-17 15:18:01
...
打包下载
Pain.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("{@", "$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
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
BR>"http://www.w3.org/TR/html4/strict.dtd">
new_file
{@$songyu@}
{@$zhangyuan@}
Pain.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("{@", "$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
复制代码 代码如下:
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
复制代码 代码如下:
BR>"http://www.w3.org/TR/html4/strict.dtd">
{@$songyu@}
{@$zhangyuan@}
上一篇: 关于javascript的包装类型对象
推荐阅读
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)
-
PHP原生模板引擎 最简单的模板引擎_PHP教程
-
PHP原生模板引擎 最简单的模板引擎_PHP教程
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)_PHP教程
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)_php模板
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)_php模板
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)_php模板_脚本之家
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)
-
Pain 全世界最小最简单的PHP模板引擎 (普通版)_php模板