zoukankan      html  css  js  c++  java
  • 写一个属于自己的模板引擎(1)

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

    开始编码了>>>

    1. <?php 
    2. define('TPL_DIR''./templates/'); 
    3. define('TPL_C_DIR''./templates_c/'); 
    4. class Stupid 
    5.         private $_tpl_vars
    6.         private $_tpl_file
    7.         private $_parser
    8.         private $_debugger
    9. ?> 


    开始写个构造器吧>>>

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

    在构造器中,我们判断了模板路径和编译路径是否设置正确.

    设计我们的方法
    我们这个类中主要有以下方法:
    assign(), set_tpl_dir(), set_parsed_dir(), display(), debug().
    assign()方法:
    assign()的用处是设置模板变量.代码如下>>>

    1. public function assign($var$value) { 
    2. if(isset($var) && trim($var) != '') { 
    3.                 $this->_tpl_vars[$var] = $value
    4.                 return true
    5.         } else { 
    6.                 exit('错误:请设置变量名'); 
    7.         } 

    我们先判断用户是否设置了变量名,用isset($var) && trim($var) != ''来判断, trim($var) != ''是防止用户以空格来设置变量名.如果设置变量正确,我们就将他保存到成员变量_tpl_vars中.

    display()方法
    display()方法是Stupid类中最重要的方法,他是用来显示和检测模板是否更新了,更新了就再编译,没有更新就用原来编译之后的文件.

    代码如下>>>

    1. public function display($tpl_file) { 
    2.   $template_file TPL_DIR.$tpl_file
    3.   if(!file_exists($template_file)) { 
    4.    exit('错误:模板文件不存在'); 
    5.   } 
    6.    
    7.   $parsed_file TPL_C_DIR.md5($tpl_file).'.php'
    8.   if(!file_exists($parsed_file) || filemtime($parsed_file) < filemtime($template_file)) { 
    9.    require_once './stupid_parser.class.php'
    10.    $this->_parser = new StupidParser(); 
    11.    $this->_parser->compile($tpl_file); 
    12.   } 
    13.   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方法进行调试.

    代码如下>>>

    1. public function debug ($tpl_file) { 
    2.         if (include_once("stupid_debugger.class.php")) { 
    3.                 $this->_debugger = new StupidDebugger($tpl_file); 
    4.                 $this->_debugger->start(); 
    5.         } else { 
    6.                 exit( '错误:Debuger类文件不存在'); 
    7.         } 

    至此,我们的Stupid类就写完了!下次我要介绍StupidParser类的编写.请继续支持.大家有什么意见或者建议可以提出!

    show show全相:

    1. <?php 
    2. define('TPL_DIR''./templates/'); 
    3. define('TPL_C_DIR''./templates_c/'); 
    4. class Stupid 
    5. private $_tpl_vars
    6. private $_tpl_file
    7. private $_parser
    8. private $_debug
    9. public function Stupid() { 
    10.   if(!is_dir(TPL_DIR) || !is_dir(TPL_C_DIR)) { 
    11.    exit('错误:请正确设置模板文件夹和编译文件夹'); 
    12.   } 
    13. public function assign($var$value) { 
    14.   if(isset($var) && trim($var) != '') { 
    15.    $this->_tpl_vars[$var] = $value
    16.    return true
    17.   } else { 
    18.    exit('错误:请设置变量名'); 
    19.   } 
    20. public function display($tpl_file) { 
    21.   $template_file TPL_DIR.$tpl_file
    22.   if(!file_exists($template_file)) { 
    23.    exit('错误:模板文件不存在'); 
    24.   } 
    25.    
    26.   $parsed_file TPL_C_DIR.md5($tpl_file).'.php'
    27.   if(!file_exists($parsed_file) || filemtime($parsed_file) < filemtime($template_file)) { 
    28.    require_once './stupid_parser.class.php'
    29.    $this->_parser = new StupidParser(); 
    30.    $this->_parser->compile($tpl_file); 
    31.   } 
    32.   include $parsed_file
    33. function debug($tpl_file) { 
    34.   if (include_once("stupid_debugger.class.php")) { 
    35.    $this->_debugger = new StupidDebugger($tpl_file); 
    36.    $this->_debugger->start(); 
    37.   } else { 
    38.    exit( '错误:Debuger类文件不存在'); 
    39.   } 
    40. ?> 


     

  • 相关阅读:
    使用JavaScript实现新闻滚动效果
    ArcGIS Server建立缓存(切图)原理解析[图解] (转载)
    tp5 中使用自定义扩展类和函数
    phpstorm 配置点右上角浏览器图标时用浏览器打开当前页面
    PHP命名空间(Namespace)的使用简介
    linux 查看和修改文件时间
    头像上传uploadPreview插件
    富文本编辑器 kindEditor
    linux 计划任务 crontab 简单用法
    阿里 短信 使用方法
  • 原文地址:https://www.cnblogs.com/kuyuecs/p/1390603.html
Copyright © 2011-2022 走看看