您好,欢迎访问一九零五行业门户网

毛毛虫教你写一个属于自己的模板引擎_PHP教程

#phpchina首发#
smarty一直被人视为是多余的东西,我觉得认为smarty多余的人才是多余的....不说这些了。今天我就教大家写个模板引擎,让大家都可以写一个属于自己的模板引擎,而且看完这篇文章之后,你对smarty的认识会更进一步的。我的模板引擎名叫stupid(傻瓜的意思),我不喜欢太聪明的东西!
stupid模板引擎是由3个文件组成,他们分别是:stupid.class.php,stupid_parser.class.php,stupid_debugger.class.php。
stupid.class.php的任务是设置变量,模板路径,和显示等功能,而stupid_parser.class.php就是编译模板文件的,stupid_debugger.class.php是用来调试用的。
好了,我们现在就先编写stupid.class.php吧。
1.新建一个php文件名为:stupid.class.php。
我们的类叫stupid,我们先设计一下成员变量吧。
成员变量有:$_tpl_vars, $_tpl_file, $_parser, $_debugger;
$_tpl_vars: 用来保存模板变量的;
$_tpl_file: 用来保存模板文件名的;
$_parser: 保存stupidparser对象的,就是编译对象;
$_debugger: 保存stupiddebug对象的,就是调试对象;
下面定义了两个常量,用来存放模板文件夹和编译文件夹的:
define(tpl_dir, ./templates/);
define(tpl_c_dir, ./templates_c/);
开始编码了>>>
开始写个构造器吧>>>
public function stupid() {
  if(!is_dir(tpl_dir) || !is_dir(tpl_c_dir)) {
   exit(错误:请正确设置模板文件夹和编译文件夹);
  }
}
在构造器中,我们判断了模板路径和编译路径是否设置正确.
设计我们的方法
我们这个类中主要有以下方法:
assign(), set_tpl_dir(), set_parsed_dir(), display(), debug().
assign()方法:
assign()的用处是设置模板变量.代码如下>>>
public function assign($var, $value) {
if(isset($var) && trim($var) != ) {
                $this->_tpl_vars[$var] = $value;
                return true;
        } else {
                exit(错误:请设置变量名);
        }
}
我们先判断用户是否设置了变量名,用isset($var) && trim($var) != 来判断, trim($var) != 是防止用户以空格来设置变量名.如果设置变量正确,我们就将他保存到成员变量_tpl_vars中.
display()方法
display()方法是stupid类中最重要的方法,他是用来显示和检测模板是否更新了,更新了就再编译,没有更新就用原来编译之后的文件.
代码如下>>>
public function display($tpl_file) {
  $template_file = tpl_dir.$tpl_file;
  if(!file_exists($template_file)) {
   exit(错误:模板文件不存在);
  }
$parsed_file = tpl_c_dir.md5($tpl_file)..php;
  if(!file_exists($parsed_file) || filemtime($parsed_file)    require_once ./stupid_parser.class.php;
   $this->_parser = new stupidparser();
   $this->_parser->compile($tpl_file);
  }
  include $parsed_file;
}
这个方法是根据!file_exists($parsed_file)||filemtime($parsed_file)
debug()方法
debugg()方法就比较简单,就是引入stupid_debugger.class.php文件,创建stupiddebuger对象,调用stupiddebuger的start方法进行调试.
代码如下>>>
public function debug ($tpl_file) {
        if (include_once(stupid_debugger.class.php)) {
                $this->_debugger = new stupiddebugger(tpl_dir. $tpl_file);
                $this->_debugger->start();
        } else {
                exit( 错误:debuger类文件不存在);
        }
}
至此,我们的stupid类就写完了!下次我要介绍stupidparser类的编写.请继续支持.大家有什么意见或者建议可以提出!
show show全相:
_tpl_vars[$var] = $value;
   return true;
  } else {
   exit(错误:请设置变量名);
  }
}
public function display($tpl_file) {
  $template_file = tpl_dir.$tpl_file;
  if(!file_exists($template_file)) {
   exit(错误:模板文件不存在);
  }
$parsed_file = tpl_c_dir.md5($tpl_file)..php;
  if(!file_exists($parsed_file) || filemtime($parsed_file)    require_once ./stupid_parser.class.php;
   $this->_parser = new stupidparser();
   $this->_parser->compile($tpl_file);
  }
  include $parsed_file;
}
function debug($tpl_file) {
  if (include_once(stupid_debugger.class.php)) {
   $this->_debugger = new stupiddebugger($this->_template_dir . $tpl_file);
   $this->_debugger->start();
  } else {
   exit( 错误:debuger类文件不存在);
  }
}
}
?>
http://www.bkjia.com/phpjc/486612.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/486612.htmltecharticle#phpchina首发# smarty一直被人视为是多余的东西,我觉得认为smarty多余的人才是多余的....不说这些了。今天我就教大家写个模板引擎,让大家...
其它类似信息

推荐信息