查看: 4916|回复: 0
打印 上一主题 下一主题

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

[复制链接]
跳转到指定楼层
1#
发表于 2009-1-2 15:59:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
台州网址导航
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类文件不存在');
  }
}
}
?>
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
台州维博网络(www.tzweb.com)专门运用PHP+MYSQL/ASP.NET+MSSQL技术开发网站门户平台系统等。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

网站推广
关于我们
  • 台州朗动科技(Tzweb.com)拥有多年开发网站平台系统门户手机客户端等业务的成功经验。主要从事:政企网站,系统平台,微信公众号,各类小程序,手机APP客户端,浙里办微应用,浙政钉微应用、主机域名、虚拟空间、后期维护等服务,满足不同企业公司的需求,是台州地区领先的网络技术服务商!

Hi,扫描关注我

Copyright © 2005-2026 站长论坛 All rights reserved

Powered by 站长论坛 with TZWEB Update Techonolgy Support

快速回复 返回顶部 返回列表