Pain 全世界最小最简单的PHP模板引擎 (普通版)
作者:bea
打包下载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 fu
打包下载
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>
有用 | 无用
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数组函数序列之prev() - 移动数组内部指针到上一个元素的位置,并返回该元素值
- php数组函数序列之next() - 移动数组内部指针到下一个元素的位置,并返回该元素值
- php数组函数序列之array_values() 获取数组元素值的函数与方法
- php数组函数序列之array_keys() - 获取数组键名
- php数组函数序列之array_combine() - 数组合并函数使用说明
- php数组函数序列之in_array() 查找数组值是否存在
- php数组函数序列之array_unique() - 去除数组中重复的元素值
- php数组函数序列之array_sum() - 计算数组元素值之和
- php数组函数序列之array_key_exists() - 查找数组键名是否存在
- php数组函数序列 之array_count_values() 统计数组中所有值出现的次数函数
- php数组函数序列 之shuffle()和array_rand() 随机函数使用介绍
- 让Json更懂中文(JSON_UNESCAPED_UNICODE)
- PHP实现异步调用方法研究与分享
- 提示Trying to clone an uncloneable object of class Imagic的解决
- PHP 小心urldecode引发的SQL注入漏洞
- Session保存到数据库的php类分享
- php中批量修改文件后缀名的函数代码
- php中经典方法实现判断多维数组是否为空
- PHP禁止页面缓存的代码