Home  >  Article  >  Backend Development  >  Caterpillar teaches you to write your own template engine_PHP tutorial

Caterpillar teaches you to write your own template engine_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:56926browse

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

开始编码了>>>

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-& gt; _debugger-& gt; start ();
} Else {
EXIT (error: Debuger file does not exist);
At this point, our Stupid class is finished! Next time I will introduce the writing of the StupidParser class. Please continue to support. If you have any comments or suggestions, please feel free to put forward!

show show full photo:

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(Error: Please set the template folder and compilation folder correctly);

}
}

public function assign($var, $value) {
if(isset($var) && trim($var) != ) {

$this->_tpl_vars[$var] = $value ;

return true;
} else {
exit(Error: Please set variable name);
}
}

public function display($tpl_file) {
$template_file = TPL_DIR.$tpl_file;

if(!file_exists($template_file)) {

exit(Error: template file does not exist);
}

$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(Error: Debuger class file does not exist);
}
}
}
?>


http://www.bkjia.com/PHPjc/486612.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486612.htmlTechArticle#phpchina开户# Smarty has always been regarded as redundant. I think people who think Smarty is redundant are redundant. .... Let’s not talk about this anymore. Today I will teach you how to write a template engine so that you can...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn