当前位置:首页 > PHP教程 > PHP常见问题

网友原创的PHP模板类代码

复制代码 代码如下:

<?php
class lightpage_template {

var $tpl_header;
var $tpl_footer;
var $tpl_parsing;
var $tpl_template;
var $tpl_dirname;

var $tpl_parse_string;
var $tpl_parse_array;

var $tpl_result;

function __construct() {
$this->tpl_header = null;
$this->tpl_footer = null;
$this->tpl_parsing = array();
$this->tpl_template = 'list.html';
$this->tpl_toparse = null;

$this->tpl_parse_string = array();
$this->tpl_parse_array = array();

$this->tpl_result = null;
return true;
}

function parse_template() {
$this->tpl_parse_string = array();
$this->tpl_parse_array = array();
if($this->tpl_header!=null) { array_push($this->tpl_parse_string,$this->tpl_header);array_push($this->tpl_parse_array,'{header}'); }
if($this->tpl_footer!=null) { array_push($this->tpl_parse_string,$this->tpl_footer);array_push($this->tpl_parse_array,'{footer}'); }
if(count($this->tpl_parsing)!=1) {
foreach($this->tpl_parsing as $tpl_key => $tpl_value) {
array_push($this->tpl_parse_string,$tpl_value);
array_push($this->tpl_parse_array,'{'.$tpl_key.'}');
}
}
if($this->tpl_template!=null && $this->tpl_toparse==null) {
$this->tpl_toparse = file_get_contents(root.'./templates/'.$this->tpl_template);
}
$this->tpl_result = str_replace($this->tpl_parse_array,$this->tpl_parse_string,$this->tpl_toparse);
return $this->tpl_result;
}

}
?>

php模板用法:
复制代码 代码如下:

$mdl = new lightpage_template();
$mdl->tpl_header = 'zzz';
$mdl->tpl_footer = '';
$mdl->tpl_parsing = '';
$mdl->tpl_template = 'list.html';
echo $mdl->parse_template();


【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!