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

王朝学院·作者佚名  2009-01-04
窄屏简体版  字體: 小  |  中  |  大  |  超大  

#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/');

开始编码了>>>

<?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(), 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) < filemtime($template_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)< filemtime($template_file)这条语句来判断是否编译过和模板文件是否更新过, 没有编译过和更新过模板文件都要重新编译.我们就要引入stupid_parser.class.php,并创建StupidParser对象,对模板文件进行编译.编译完,我们就引入编译之后的文件.这个编译之后的模板文件就是一个普通的PHP文件.

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全相:

<?php

define('TPL_DIR', './templates/');

define('TPL_C_DIR', './templates_c/');

class Stupid {

private $_tpl_vars;

private $_tpl_file;

private $_parser;

private $_debug;

public function Stupid() {

if(!is_dir(TPL_DIR) || !is_dir(TPL_C_DIR)) {

exit('错误:请正确设置模板文件夹和编译文件夹');

}

}

public function assign($var, $value) {

if(isset($var) && trim($var) != '') {

$this->_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) < filemtime($template_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类文件不存在');

}

}

}

?>

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航