欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

网友原创的PHP模板类代码

程序员文章站 2023-12-02 17:06:46
复制代码 代码如下:

复制代码 代码如下:

<?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();