毛毛蟲教你寫一個屬於自己的模板引擎_PHP教程

來源:互聯網
上載者:User

#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->_debugger->start();
} else {
exit( 錯誤:Debuger類檔案不存在);
}
}

至此,我們的Stupid類就寫完了!下次我要介紹StupidParser類的編寫.請繼續支援.大家有什麼意見或者建議可以提出!

show show全相:

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類檔案不存在);
}
}
}
?>

http://www.bkjia.com/PHPjc/486612.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486612.htmlTechArticle#phpchina首發# Smarty一直被人視為是多餘的東西,我覺得認為Smarty多餘的人才是多餘的....不說這些了。今天我就教大家寫個模板引擎,讓大家...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.