0
点赞
收藏
分享

微信扫一扫

写一个属于自己的模板引擎【一】

老牛走世界 2022-12-01 阅读 29


参考文档《毛毛虫教你写一个属于自己的模板引擎》,自己也模仿着写一遍,原谅我比较懒,具体详解请百度原作者作品。

stupid.class.php

<?php 
define('TPL_DIR','./templates/');
define('TPL_C_DIR','./templates_c/');
class Stupid{
private $_tpl_vars; //模板变量
private $_tpl_file; //模板文件名
private $_parser; //编译对象
private $_debugger; //调试对象

public function Stupid(){
if( !is_dir(TPL_DIR) || !is_dir(TPL_C_DIR) ){
exit("错误:请正确设置模板文件和编译文件夹!");
}
}

//assign
public function assign($var,$value){
if( isset($var)&&trim($var)!='' ){
$this->_tpl_vars[$var] = $value;
}else{
exit("错误:请设置变量名!");
}
}

//display
public function display($tpl_file){
$template_file = TPL_DIR.$tpl_file."php";
if( !file_exists($template_file) ){
exit("模板文件不存在!");
}

$parsed_file = TPL_C_DIR.md5($tpl_file)."php";
//编译模板文件
if( !file_exists($parsed_file) || filemtime($parsed_file)<filemtime($template_file) ){
require_once './stupid_parser.class.php';
$this->_parser = new StupidParser();
$this->_parser->compile($tpl_file);
}
include $parsed_file;
}

//debug()方法
public function debug($tpl_file){
if( include_once("stupid_debugger.class.php") ){
$this->_debugger = new StupidDebugger($tpl_file);
$this->_debugger->start();
}else{
exit("错误:Debugger类不存在");
}
}

}




举报

相关推荐

0 条评论