自定義min版smarty模板引擎MinSmarty.class.php文件及用法,smarty模板引擎_PHP教程
Jul 12, 2016 am 08:50 AM自定義min版smarty模板引擎MinSmarty.class.php文件及用法,smarty模板引擎
本文實例講述了自定義的min版smarty模板引擎MinSmarty.class.php文件。分享給大家供大家參考,具體如下:
一、smarty的優(yōu)點
smarty是一個使用PHP寫出來的模板引擎,是目前業(yè)界最著名的PHP模板引擎之一。它分離了邏輯代碼和外在的內(nèi)容,提供了一種易于管理和使用的方法,用來將原本與HTML代碼混雜在一起PHP代碼邏輯分離。簡單的講,目的就是要使PHP程序員同前端人員分離,使程序員改變程序的邏輯內(nèi)容不會影響到前端人員的頁面設(shè)計,前端人員重新修改頁面不會影響到程序的程序邏輯,這在多人合作的項目中顯的尤為重要。
二、寫一個簡單的smarty模版類
具體代碼如下:
<?php class MinSmarty{ // 模版文件的路徑 var $template_dir = "./templates/"; // 模版文件被替換后的文件 命名格式為com_對應(yīng)的tpl.php var $complie_dir = "./templates_c/"; // 存放變量值 var $tpl_vars = array(); // 這里使用兩個方法實現(xiàn)assign 和 display function assign($tpl_var,$var=NULL){ if($tpl_var!=NULL){ $this->tpl_vars[$tpl_var]=$var; } } // 這里編寫display方法的實現(xiàn) function display($tpl_file){ // 讀取這個模版文件->替換可以運行的php文件(編譯后文件) $tpl_file_path=$this->template_dir.$tpl_file; // 模版文件的路徑 $complie_file_path=$this->complie_dir."com_".$tpl_file.".php"; //編譯后的文件路徑 // 判斷文件是否存在 if(!file_exists($tpl_file_path)){ return false; } // 不讓每次執(zhí)行都生成編譯文件 if(!file_exists($complie_file_path) || filemtime($tpl_file_path)>filemtime($complie_file_path)){ $fp1_file_con=file_get_contents($tpl_file_path); // 獲取模版文件的全部內(nèi)容 // 這里進(jìn)行正則替換把 模版文件中的代碼 {$title} 替換成 <?php echo $this->tpl_vars['title'];? > $pattern=array( '/\{\s*\$([a-zA-Z_][a-zA-Z0-9_]*)\s*\}/i' ); $replace=array( '<?php echo $this->tpl_vars["${1}"];?>' ); $new_str=preg_replace($pattern,$replace,$fp1_file_con); // 替換后的內(nèi)容 file_put_contents($complie_file_path,$new_str); // 替換后的內(nèi)容生成一個php文件 } // 引入編譯后的文件 include_once("$complie_file_path"); } } ?>
下面的代碼是對該類的測試
intro.php代碼如下:
<?php include_once("MySmarty.class.php"); $title="這里是標(biāo)題"; $content="這里是內(nèi)容111111"; $MySmarty=new MySmarty(); $MySmarty->assign("title",$title); $MySmarty->assign("content",$content); $MySmarty->display("intro.tpl"); ?>
模版如下:
intro.tpl:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>{$title}</title> </head> <body> {$content} </body> </html>
PS:這里推薦幾款本站的格式化/美化/轉(zhuǎn)換工具可以幫助你整理雜亂無章的代碼,相信大家在以后的開發(fā)中能夠用得上:
php代碼在線格式化美化工具:
http://tools.jb51.net/code/phpformat
JavaScript代碼美化/壓縮/格式化/加密工具:
http://tools.jb51.net/code/jscompress
在線XML格式化/壓縮工具:
http://tools.jb51.net/code/xmlformat
JSON代碼格式化美化工具:
http://tools.jb51.net/code/json
在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson
json代碼在線格式化/美化/壓縮/編輯/轉(zhuǎn)換工具:
http://tools.jb51.net/code/jsoncodeformat
sql代碼在線格式化美化工具:
http://tools.jb51.net/code/sqlcodeformat
更多關(guān)于Smarty相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《smarty模板入門基礎(chǔ)教程》、《PHP模板技術(shù)總結(jié)》、《PHP基于pdo操作數(shù)據(jù)庫技巧總結(jié)》、《PHP運算與運算符用法總結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于smarty模板的PHP程序設(shè)計有所幫助。

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

Netflix? ???? ??? ???? ???? ????? ??? ????. ???? ?? ???? ?? ??? ??? ??? ? ????. Netflix ??? ??? ?? ??? ??? ???? ??? ????? ? ??? ?? ?????. Netflix?? ??? ?? ???? ??? ???? ?? Netflix?? ??? ??? ???? ??? ???? ?? ????. ??? ????? Netflix ?? ????? ???? ? ??? ??? ? ????. ??, ????? Netflix ?? ????? ?? ??? ?? ??? ??? ?????. Chrome ????? ???? ? ????. ?? ????? ??? ? ?????? Netflix? ?? ??? ??????. ??? ??? ?? ???? ????

? ?????? ?? ?? ??? ???? ? ???? ????????. Venn ?????? ????? matplotlib? ?????. Matplotlib? Python?? ??? ??? ???? ??? ?? ????? ???? ??? ??? ????????. ?? ??? ???? ??? ??? ??? ?????. Matplotlib? ??? ???? ??? ??? ? ?? ??? ??? ?????. ? ??????? ? ?????? ??? ???? ? ?? ?? ?????. ??? ??? ??? ??? ????. ?? ??? ? ?? ? ?????? ???? ??? ??? ????. ?? ??? ?????? ???? ?? ??????. ?? ?? ??? ??? Python ??? ??? ? "venn2()" ??? ???? ?????.

Win11?? ?? ???? ??? ???? ??? ?????? ?? ??? win11 ????? ??? ??? ?? ???, ?? ??? ??? ????? ??? ???? ????. ?? ???? ?? ???? ????? ????? ???? ?? ???? ??? ???? ??? ?? ???? ???? ??? ??? ?? ????? ?? ??? ???????. ?? Win11?? ?? ???? ??? ?????. ??? ??? ??? ?????! Win11?? ?? ???? ??? ???? ??: 1. ??? ?? ?? ???? win ??? ???? ?? ???? ??? ?????. 2. ??? ?? ?? ??? ???? ?? ??? ?????. 3. ??? ?? ?? ??? ???? ??? ?????. 4. ?? ??? ???? ???? ??? ?????.

iPhone? iOS 17 ????? Apple Music? ? ?? ? ??? ??????. ???? ?? ???? ?? ???? ?? ??, CarPlay ?? ? ?? ???? ?? ?? ?? ?? ?????. ??? ??? ?? ? ??? Apple Music?? ??????? ???? ?????. ?? ?? ?? ?? ???? ??? ? ???, ?? ?? ??? ?? ? ?? ??? ?????. ??????? ???? ?? ??? ???? ??? ??? ? ???? ???? ???? ?? ??? ?????. ??? ? ??? ??? ??? ???? ??? iPhone?? ? ??? ???? ??? ??? ????. ??? ??? Apple Music? Crossfade? ????? ??? ???? ??

CakePHP? ????? ?? ??? ??? ??? ???? ??? PHP ????????. ? ? ??? ??? ?????. ?? ?? ??? ???? ?? ???? ??? ??? ??? ? ?? ? ? ????. ????? CakePHP? ? ?? ???? ??? ?? ??? ????? ??? ??? ?? ??? ?? ??? ???? ? ?? ????. ? ????? CakePHP?? ??? ?? ??? ??? ??? ??? ?????. 1??: ??? ?? ??? ?? ??? ?? ?? ??? ?? ??? ?? ???? ???? ???. ??

Eclipse?? ?? ?? ? ??? ??? ???? ??? ?????? ????? ???? ??? ?? Eclipse?? ??? ? ???? ??? ?? ? ?????. ??? ?? ?? ??? Eclipse? ??? ?? ???? ?? ??? ?? ??? ???? ??? ?? ??? ?? ??? ??? ?? ????. ? ????? Eclipse?? ?? ?? ? ??? ??? ???? ??? ???? ?? ?? ??? ?????. Eclipse ?? ?? Eclipse? ?? Enter? ?????.

CodeIgniter?? ??? ?? ????? ???? ?? ??: ?? ? ???? ????? ???????? ??? ??? ???. ??? ????? ???? ??? ?? ?? ?? ?? ??? ???? ? ??? ? ????. ?? ???? PHP ?????? CodeIgniter? ???? ??? ?????. ? ???? CodeIgniter?? ??? ?? ????? ???? ??? ???? ??? ?? ??? ?????. ???? ??: ????? ??? ?????.

1. ?? ??? edius? ?? ?? ???????. ?? EDIUS ? ????? ?? ??????? ?? ??? ????? ?? ?? ??? ??? ???? ?? ?? ? ?????. 2. [??] ?? ?? ?? [?? ??? ??]? ????? ???? ?? ?? ?? ?? ?? ??? ????? ? ? ????. 3. [?? ???>? ????>??]? ?? ?? ?? ????? ??? ? ????. ??, ???? ?? ????? ??? ???? ?? ???? ?? ?????? ??? ?? ????. ?? ???? ?? ?????? ???? ? [?? > ? ???? > ?? ???? ?? > ?? ???]? ??? ? ?? [?? ???? ??] ????] ?? ?? ???? ??? ???? ??? ?????.
