国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

毛毛蟲教你寫一個屬于自己的模板引擎

2019-11-17 04:21:39
字體:
來源:轉載
供稿:網友

#phpchina首發#

Smarty一直被人視為是多余的東西,我覺得認為Smarty多余的人才是多余的....不說這些了。今天我就教大家寫個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類文件不存在');
  }
}
}
?>


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 容城县| 达尔| 灵石县| 梁山县| 义马市| 沈阳市| 乌拉特前旗| 龙南县| 淳安县| 桃源县| 河东区| 瑞丽市| 岳池县| 宁化县| 阳江市| 广水市| 临沂市| 安徽省| 怀远县| 商南县| 乌兰浩特市| 西安市| 泽库县| 白沙| 汝南县| 石棉县| 宁安市| 亳州市| 泌阳县| 玉林市| 蕉岭县| 巴中市| 义乌市| 陆丰市| 瑞安市| 平果县| 沂南县| 什邡市| 菏泽市| 宜川县| 杨浦区|