自娱自乐--超简易模版类
时间:2006-01-10 来源:放弃思考
今晚闲着无聊,写了个简易的模板类:
Class EasyTpl{
var $html;
function EasyTpl ($file)
{
$this->html = file_get_contents( $file );
}
//一般变量替换方法
function setVar ($var,$replace)
{
$content = &$this->html;
$content = str_replace( '{'.$var.'}',$replace,$content );
}
//用来处理一维数组,如Array ( [id] => 19 [name] => test [city] => 厦门 )
function setArray ($array_name,$replace)
{
$content = &$this->html;
foreach ( $replace as $key => $value )
{
$content = str_replace( '{'.$array_name.'.'.$key.'}',$value,$content );
}
}
/*用来处理多维数组(常见数据库查询结果)
Array ( [0] => Array ( [id] => 19 [name] => test [city] => 厦门 ) [1] => Array ( [id] => 20 [name] => ffffff [city] => 北京 ) )
*/
function setArrayM ($loop,$replace)
{
$content = &$this->html;
$start_position = strPos( $content,'{'.$loop.'}');
$end_position = strPos($content,'{/'.$loop.'}') + strLen('{/'.$loop.'}');
$string = subStr( $content,$start_position,$end_position-$start_position);//获取循环段
$sub_string = subStr( $string,strLen( '{'.$loop.'}' ),strPos($string,'{/'.$loop.'}')-strLen( '{'.$loop.'}' )); //取出循环段中字符
preg_match_all('#\{(.+?)\}#',$sub_string,$search);//获得要替换的数组变量
foreach ( $replace as $key => $value )
{
$search_array = array();
$replace_array = array();
foreach ( $value as $name => $value2 )
{
if ( in_array( $name,$search[1] ) )
{
$search_array[] = '{'.$name.'}';
$replace_array[] = $value2;
}
}
$temp .= str_replace( $search_array,$replace_array,$sub_string );
}
$content = str_replace( $string,$temp,$content );
}
function display ()
{
echo $this->html;
}
}
?>
示例:
html文件--test.htm
1 setVar方法测试:
你好:{user}
2 setArray方法测试:
{data1.id}{data1.name}{data1.city}
3 setArrayM方法测试:
{data2}
{id}{name}{city}
{/data2}
php文件:
$data1=array(
'id'=>19,
'name'=>'test',
'city'=>'厦门'
);
$data2=array(
array('id'=>19,'name'=>'test','city'=>'厦门'),
array('id'=>20,'name'=>'ffffff','city'=>'北京')
);
$view = new easyTpl('./test.htm');//调用模板类并指定html文件
$view->setVar('user',"放弃思考");//一般变量替换
$view->setArray('data1',$data1);//一维数组替换
$view->setArrayM('data2',$data2);//多维数组替换
$view->display();
?>
PS:该类未经严格测试,且效率未知,哈哈
源码及示例下载:
文件:
EasyTpl_v1.0.0.rar
大小:
1KB
下载:
下载
Class EasyTpl{
var $html;
function EasyTpl ($file)
{
$this->html = file_get_contents( $file );
}
//一般变量替换方法
function setVar ($var,$replace)
{
$content = &$this->html;
$content = str_replace( '{'.$var.'}',$replace,$content );
}
//用来处理一维数组,如Array ( [id] => 19 [name] => test [city] => 厦门 )
function setArray ($array_name,$replace)
{
$content = &$this->html;
foreach ( $replace as $key => $value )
{
$content = str_replace( '{'.$array_name.'.'.$key.'}',$value,$content );
}
}
/*用来处理多维数组(常见数据库查询结果)
Array ( [0] => Array ( [id] => 19 [name] => test [city] => 厦门 ) [1] => Array ( [id] => 20 [name] => ffffff [city] => 北京 ) )
*/
function setArrayM ($loop,$replace)
{
$content = &$this->html;
$start_position = strPos( $content,'{'.$loop.'}');
$end_position = strPos($content,'{/'.$loop.'}') + strLen('{/'.$loop.'}');
$string = subStr( $content,$start_position,$end_position-$start_position);//获取循环段
$sub_string = subStr( $string,strLen( '{'.$loop.'}' ),strPos($string,'{/'.$loop.'}')-strLen( '{'.$loop.'}' )); //取出循环段中字符
preg_match_all('#\{(.+?)\}#',$sub_string,$search);//获得要替换的数组变量
foreach ( $replace as $key => $value )
{
$search_array = array();
$replace_array = array();
foreach ( $value as $name => $value2 )
{
if ( in_array( $name,$search[1] ) )
{
$search_array[] = '{'.$name.'}';
$replace_array[] = $value2;
}
}
$temp .= str_replace( $search_array,$replace_array,$sub_string );
}
$content = str_replace( $string,$temp,$content );
}
function display ()
{
echo $this->html;
}
}
?>
示例:
html文件--test.htm
1 setVar方法测试:
你好:{user}
2 setArray方法测试:
{data1.id}{data1.name}{data1.city}
3 setArrayM方法测试:
{data2}
{id}{name}{city}
{/data2}
php文件:
$data1=array(
'id'=>19,
'name'=>'test',
'city'=>'厦门'
);
$data2=array(
array('id'=>19,'name'=>'test','city'=>'厦门'),
array('id'=>20,'name'=>'ffffff','city'=>'北京')
);
$view = new easyTpl('./test.htm');//调用模板类并指定html文件
$view->setVar('user',"放弃思考");//一般变量替换
$view->setArray('data1',$data1);//一维数组替换
$view->setArrayM('data2',$data2);//多维数组替换
$view->display();
?>
PS:该类未经严格测试,且效率未知,哈哈
源码及示例下载:
文件:
EasyTpl_v1.0.0.rar
大小:
1KB
下载:
下载
相关阅读 更多 +