一,创建初始化模板所需的文件和文件夹。
1,index.php主文件,用于编写业务逻辑。
2,template.inc.php模板初始化文件,用于初始化模板信息。
3,templates目录存放所有的模板文件。
4,templates_c目录存放所有的模板编译文件。
5,cache目录存放所有的缓存文件。
6,includes目录存放所有的类文件、
7,config目录存放模板系统变量配置文件。
二,初始化template.inc.php文件
<?php
//设置编码为utf-8
header('Content-Type:text/html;charset=utf-8');
//网站根目录
define('ROOT_PATH', dirname(__FILE__));
//存放模板文件夹
define('TPL_DIR', ROOT_PATH.'/templates/');
//编译文件夹
define('TPL_C_DIR', ROOT_PATH.'/templates_c/');
//缓存文件夹
define('CACHE_DIR', ROOT_PATH.'/cache/');
define('IS_CACHE', true);
IS_CACHE ? ob_start():null;
include ROOT_PATH.'./includes/Templates.class.php';
?>
三,初始化Template.class.php模板类和Parser.class.php模板解析类
class Templates{
}
class Parser{
}
<?php
/**
*
*/
class Templates
{
function __construct()
{
if (!is_dir(TPL_DIR) || !is_dir(TPL_C_DIR) || !is_dir(CACHE_DIR)) {
exit('ERROR:模板文件或者编译文件或者就缓存文件不存在!!');
}
//获取系统环境变量
$_sxe = simplexml_load_file(ROOT_PATH.'/config/profile.xml');
$_taglib = $_sxe->xpath('/root/taglib');
foreach ($_taglib as $_tag) {
$this->_config["$_tag->name"] = $_tag->value;
}
}
//将模板导入到PHP文件中
public function display($_file){
//设置模板文件的路径
$_tplFile = TPL_DIR.$_file;
//判断模板文件是否存在
if (!file_exists($_tplFile)) {
exit('ERROR:模板文件不存在!!');
}
//设置编译文件的文件名
$_parFile = TPL_C_DIR.md5($_file).'.php';
$_cacheFile = CACHE_DIR.md5($_file).'.html';
if (IS_CACHE) {
if (file_exists($_cacheFile) && file_exists($_parFile)) {
if (filemtime($_parFile)>=filemtime($_tplFile)&&filemtime($_cacheFile)>=filemtime($_parFile)) {
echo '正在浏览缓存文件';
include $_cacheFile;
return;
}
}
}
//判断编译文件是否存在,模板文件是否修改过
if (!file_exists($_parFile) || filemtime($_parFile)<filemtime($_tplFile)) {
require ROOT_PATH.'./includes/Parser.class.php';
$_parser = new Parser($_tplFile);
$_parser->compile($_parFile);
}
include $_parFile;
if (IS_CACHE) {
file_put_contents($_cacheFile,ob_get_contents());
ob_end_clean();
include $_cacheFile;
}
}
//创建一个存放数值的字段
private $_vars = array();
//创建变量的注入方法
public function assign($_var,$_value){
//$_var表示模板里的变量名,$_value文件的声明的变量值。
if (isset($_var) && !empty($_var)) {
$this->_vars[$_var] = $_value;
}else{
exit('ERROR:请设置变量名!');
}
}
}
?>
模板类中构造函数判断刚健的几个文件夹是否存在。
初始化XML转为关联数组。方便以后的读取。
display函数调用模板,判断传过来的模板文件是否存在,然后MD5对文件名进行加密编码,后缀为php的编译文件。
再 生成缓存文件,后缀是静态html文件,通过判断缓存文件和编译文件的修改时间比较,是否要载入缓存文件,filemtime查看时间,
缓存文件和模板文件比较时间,则浏览缓存文件。return结束。
否则,载入Parser类,来创建编译文件。获得当前输出放到缓存文件。
assign()函数对变量赋值。私有全局数组保存变量。
//创建一个存放数值的字段
private $_vars = array();
//创建变量的注入方法
public function assign($_var,$_value){
//$_var表示模板里的变量名,$_value文件的声明的变量值。
if (isset($_var) && !empty($_var)) {
$this->_vars[$_var] = $_value;
}else{
exit('ERROR:请设置变量名!');
}
}
全局数组可在解析类Parser中获得。
//判断编译文件是否存在,模板文件是否修改过
if (!file_exists($_parFile) || filemtime($_parFile)<filemtime($_tplFile)) {
require ROOT_PATH.'./includes/Parser.class.php';
$_parser = new Parser($_tplFile);
$_parser->compile($_parFile);
}
编译文件不存在或者模板文件有更新则调用 Parser类中的构造函数获得模板文件所有内容放到解析类中的私有变量,供其他函数用。
//对外入口,通过Templates.class.php来引入
public function compile($_parFile){
//生成编译文件
$this->parVar();
$this->parIf();
$this->parCommon();
$this->parForeach();
$this->parInclude();
if (!file_put_contents($_parFile, $this->_tpl)) {
exit('ERROR:编译文件生成失败!');
}
}
由上函数保存编译文件,并且他还调用其他解析函数如:
//解析普通变量
private function parVar(){
//普通变量是否有变量存在
$_patten = '/\{\$([\w]+)\}/';
if (preg_match($_patten,$this->_tpl)) {
$this->_tpl = preg_replace($_patten,"<?php echo \$this->_vars['$1'];?>",$this->_tpl);
}
}
正则表达式preg_match匹配,然后preg_replace替换。 注意,特殊字符需要转义如$符号,其中$1代表正则表达式中第一个模块的值也就是
([\w]+)值依次$2([\w]+)的值。
//解析if语句
private function parIf(){
//开头模式
$_pattenIf = '/\{if\s+\$([\w]+)\}/';
//结尾if模式
$_pattenEndIf = '/\{\/if\}/';
$_pattenElse = '/\{else\}/';
//判断一下是否有if语句
if (preg_match($_pattenIf,$this->_tpl)) {
//判断是否有结尾if
if (preg_match($_pattenEndIf,$this->_tpl)) {
$this->_tpl = preg_replace($_pattenIf,"<?php if(\$this->_vars['$1']){?>",$this->_tpl);
$this->_tpl = preg_replace($_pattenEndIf,"<?php }?>",$this->_tpl);
//判断是否有else
if (preg_match($_pattenElse,$this->_tpl)) {
$this->_tpl = preg_replace($_pattenElse,"<?php } else {?>",$this->_tpl);
}
}else{
exit('ERROR:IF语句没有关闭!!');
}
}
}
注意:匹配是不需代码的位置,if在前在后都行。还有else语句注意。
//解析foreach语句
private function parForeach(){
//搜索foreach语句的开始
$_pattenForeach = '/\{foreach\s+\$([\w]+)\(([\w]+)\,([\w]+)\)\}/';
//搜索foreach语句的结尾
$_pattenEndForeach = '/\{\/foreach\}/';
//搜索foreach开始语句
if(preg_match($_pattenForeach,$this->_tpl)) {
//搜索foreach结束语句
if (preg_match($_pattenEndForeach,$this->_tpl)) {
$this->_tpl = preg_replace($_pattenForeach,"<?php foreach(\$this->_vars['$1'] as \$$2=>\$$3){?>",$this->_tpl);
$this->_tpl = preg_replace($_pattenEndForeach,"<?php } ?>",$this->_tpl);
$this->_tpl = preg_replace('/\{\@([\w]+)\}/',"<?php echo \$$1;?>",$this->_tpl);
}else{
exit("ERROR:foreach语句没有关闭!!");
}
}
}
//解析includ语句
private function parInclude(){
//判断是否有include包含的语句
$_patten = '/\{include \"(.*)\"\}/';
if (preg_match($_patten,$this->_tpl,$_file)) {
if (empty($_file[1]) || !file_exists($_file[1])) {
echo $_file[1];
exit('ERROR:文件包含有误!!');
}
$this->_tpl = preg_replace($_patten,"<?php include '{$_file[1]}';?>",$this->_tpl);
}
}
注意:preg_match($_patten,$this->_tpl,$_file)函数是可以带第三个参数$_file用来保存每次匹配的值第一次是include “text.php” 第二次是(.*)的
内容,分别放到数组中。
//解析注释语句
private function parCommon(){
if (preg_match('/\{#}/',$this->_tpl)) {
$_patten = '/\{#\}(.*)\{#\}/';
$this->_tpl = preg_replace($_patten,"/* $1*/",$this->_tpl);
}
}