只是随便写的一个解释型的玩玩,嘻嘻
建立一个tpl.php文件内容如下
PHP 代码:
<?php
/*******************************************************
作者:斌子(顽主)
简单解释型模板引擎
*******************************************************/
class tpl{
var $vname = array();
var $filecode = array();
var $fdir = './';
var $lift = '{';
var $right = '}';
var $forlift = '{!';
var $forright = '!}';
var $str = NULL;
var $cont = NULL;
var $codeend = NULL;
var $code = NULL;
function __construct($filename)
{
$fp = fopen($this -> fdir.$filename,'r');
flock($fp,LOCK_EX);
$this -> filecode = fread($fp,filesize($this -> fdir.$filename));
flock($fp,LOCK_UN);
fclose($fp);
}
function assign($name,$value)
{
if(is_array($value))
{
list($this -> filecode,$this -> cont,$this -> codeend) = explode($this -> forlift.$name.$this -> forright,$this -> filecode);
foreach($value as $key => $values)
{
$strcode = $this -> cont;
if(is_array($values))
{
foreach($values as $k => $v)
{
$strcode = str_replace($this -> lift.$k.$this -> right,$v,$strcode);
}
}
$this -> code .= $strcode;
}
$this -> filecode .= $this -> code.$this -> codeend;
}
else
{
$this -> str = $value;
$this -> filecode = str_replace($this -> lift.$name.$this -> right,$this -> str,$this -> filecode);
}
}
function play()
{
echo $this -> filecode;
}
}
?>
使用方法,建立一个a.php文件,内容如下
PHP 代码:
<?php
include_once('tpl.php');
$tl = new tpl('q.html');
$bin = array(array('de'=>'binzi','wang'=>'dddd'),
array('de'=>'ggggg','wang'=>'hhhhh'),
array('de'=>'binzi','wang'=>'dddd'));
$binzi = 'hahhahah';
$d = 'fffff';
$tl -> lift = '{';
$tl -> right = '}';
$tl -> assign('user',$bin);
$tl -> assign('huanghe',$binzi);
$tl -> assign('d',$d);
$tl -> play();
?>
建立一个q.html文件
内容如下
HTML 代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
{huanghe}<br>{d}
<table width="900" border="1">
{!user!}
<tr>
<td>{de}</td>
<td>{wang}</td>
</tr>
{!user!}
</table>
</body>
</html>
直接预览a.php文件就可以了。