Smarty模板類內部原理實例分析
本文實例講述了Smarty模板類內部原理。分享給大家供大家參考,具體如下:
之前在學習ThinkPHP的時候,有接觸到Smarty模板類,但是一直不知道其內部實現(xiàn)的原理,博主今天終于知道了其內部原理,其實也挺簡單的,然后寫了一個迷你版的Smarty模板類,對理解其內部原理有了很大的幫助。
1、迷你版Smarty類
首先上代碼,最后再進行講解。
項目結構圖

MiniSmarty類代碼(MiniSmarty.class.php)
<?php
/**
* 迷你模板類
*/
class MiniSmarty{
public $template_dir = '';//模板文件放置的目錄
public $compile_dir = '';//編譯后文件放置的目錄
public $tpl_var = array();//模板賦值的變量
/**
* 給模板進行賦值
* @param str $key 鍵
* @param mixed $value 值
* @return void
*/
public function assign($key,$value){
$this->tpl_var[$key] = $value;
}
/**
* 編譯模板,并引入編譯后的文件
* @param str $template 模板文件
* @return void
*/
public function display($template){
$compile_file = $this->compile($template);
include($compile_file);
}
/**
* 將模板文件編譯成php文件
* @param str $template 模板文件名
* @return str 編譯文件名
*/
private function compile($template){
$template_file = $this->template_dir.'/'.$template;
//讀取模板文件中的內容
$source = file_get_contents($template_file);
//判斷是否需要再次生產編譯文件
$compile_file = $this->compile_dir.'/'.$template.'.php';
//如果存在編譯文件且編譯文件的修改時間比模板文件大,則不用再次編譯,直接返回文件路徑
if(file_exists($compile_file) && filemtime($compile_file) > filemtime($template_file)){
return $compile_file;
}
//解析{$}為<?php echo 等操作
$source = str_replace('{$', '<?php echo $this->tpl_var[\'', $source);
$source = str_replace('}', '\'];?>', $source);
//生成編譯文件
file_put_contents($compile_file, $source);
//返回編譯后的文件路徑
return $compile_file;
}
}
?>
測試模板類代碼(testSmarty.php)
<?php
//1、引入并創(chuàng)建模板實例
include ('./MiniSmarty.class.php');
$Smarty = new MiniSmarty();
$Smarty->template_dir = './template';
$Smarty->compile_dir = './compile';
//2、給模板對象賦值
$title = '兩會召開';
$content = '好奶粉,好會議,好新聞';
$Smarty->assign('title',$title);
$Smarty->assign('content',$content);
//3、顯示模板
$template = 'template.html';
$Smarty->display($template);
?>
模板文件(template.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>{$title}</title>
<link rel="stylesheet" href="">
</head>
<body>
<h3>{$content}</h3>
</body>
</html>
編譯后的文件(template.html.php)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title><?php echo $this->tpl_var['title'];?></title> <link rel="stylesheet" href=""> </head> <body> <h3><?php echo $this->tpl_var['content'];?></h3> </body> </html>
代碼都貼完了,最后解釋一下。在測試模板類(testSmarty.php)文件中,首先是引入模板類文件,實例化模板對象,然后給模板對象賦值,最后顯示模板。在模板類(MiniSmarty.class.php)文件中,有3個屬性和3個方法,屬性分別是template_dir 、compile_dir‘和tpl_var,含義分別是模板文件的路徑、編譯后文件的路徑、模板對象的變量。3個方法分別是assign、display和compile,assign方法是給模板對象賦值,display方法是編譯模板文件,并引入(顯示)編譯后的文件,compile方法是編譯模板文件。編譯模板文件的過程主要是將模板文件中的{$標簽}解析成<?php echo $var?> 等php代碼。
2、Smarty原理分析
工作流程
(1)把需要顯示的全局變量,賦值,塞到對象的內部屬性中的一個數(shù)組里
(2)然后編譯模板,將{$標簽}解析成相應的php echo 代碼
(3)引入編譯后的php文件
使用步驟
(1)Smarty是一個類,要使用的話,必須引入在進行實例化
(2)使用assign給模板賦值
(3)使用display方法【從編譯到輸出】
Smarty的缺點
(1)編譯模板,浪費時間
(2)要把變量再重新賦值到對象的屬性中,增大了開銷
更多關于Smarty相關內容感興趣的讀者可查看本站專題:《smarty模板入門基礎教程》、《PHP模板技術總結》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結》、《PHP運算與運算符用法總結》、《PHP網(wǎng)絡編程技巧總結》、《PHP基本語法入門教程》、《php面向對象程序設計入門教程》、《php字符串(string)用法總結》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于smarty模板的PHP程序設計有所幫助。
相關文章
Drupal讀取Excel并導入數(shù)據(jù)庫實例
這篇文章主要介紹了Drupal利用PHPExcel讀取Excel并導入數(shù)據(jù)庫的例子,需要的朋友可以參考下2014-03-03
tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結
這篇文章主要介紹了tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作,結合實例形式總結分析了thinkPHP5框架數(shù)據(jù)庫的增刪改查常見操作技巧與相關注意事項,需要的朋友可以參考下2019-01-01
PHP數(shù)組的基本操作及遍歷數(shù)組的經(jīng)典操作實例
下面小編就為大家?guī)硪黄狿HP數(shù)組的基本操作及遍歷數(shù)組的經(jīng)典操作實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08
thinkphp5框架實現(xiàn)數(shù)據(jù)庫讀取的數(shù)據(jù)轉換成json格式示例
這篇文章主要介紹了thinkphp5框架實現(xiàn)數(shù)據(jù)庫讀取的數(shù)據(jù)轉換成json格式,涉及thinkPHP5數(shù)據(jù)庫讀取數(shù)據(jù)與json格式轉換相關操作技巧,需要的朋友可以參考下2019-10-10

