Re: [請益] smarty樣板關聯剖析
自問自答, 剛試寫一下, 先把骨幹弄出來:
<?php
//smarty樣板關聯剖析器
smarty_template_parser($argv[1]);
function smarty_template_parser($filename,$layer=0){
//0 記下樣版檔的路徑名稱:
$dir=dirname($filename);
//1 以'前綴空格'的方式代表階層結構, 印出様版檔名:
printf("%s%s\r\n",mk_blanks($layer),basename($filename));
//2 樣板內容處理,主要是消除掉換行字符:
$contents=file_get_contents($filename);
$contents=str_replace("\r\n","\n",$contents);
$contents=str_replace("\n",'',$contents);
//3 樣式比對, 抓出子様版的檔名:
preg_match_all("/\<\{include[\s]*file='([^']+?)'\}\>/i",$contents,$result);
//4 以遞迴方式對於每一個子様版做剖析
foreach($result[1] as $child):
smarty_template_parser($dir.'/'.$child,$layer+1);
endforeach;
}
//製作空白格字串:
function mk_blanks($num){
$blanks='';
for($i=0;$i<$num;$i++)
$blanks.=' ';
return $blanks;
}
?>
範例輸出:
php -q smarty_analyzer.php templates\page_home.tpl
page_home.tpl
_block_banner1.tpl
_block_boy_view.tpl
_block_query_advanced.tpl
_block_query_basic.tpl
frame_right.tpl
page_front_base.tpl
frame_head.tpl
_block_logo.tpl
_block_menu_nav.tpl
frame_left.tpl
_panel_login.tpl
_block_login.tpl
_panel_plurk.tpl
_panel_announcements.tpl
_panel_sitelinks.tpl
_panel_google_adv.tpl
_block_google_adv.tpl
frame_footer.tpl
※ 引述《bobju (寶貝豬)》之銘言:
: 請教關於smarty的功能:
: smarty可有提供方法, 可將所有樣板的關聯結構呈現出來? 只要能抓
: 有 include 關聯的就行了.
: 例如:
: a.tpl的內容裏含有<{include file='b.tpl'}>, 及<{include file='c.tpl'}>,
: 而b.tpl的內容裏含有<{include file='d.tpl'}>
: 則有沒有辦法取得如下所示意的(樹狀)資料結構 或是 輸出結果?
: a.tpl
: b.tpl
: d.tpl
: c.tpl
: 若smarty沒有像這樣的功能的話, 那麼可有相關的php 程式可以做得到呢?
: 謝謝看完. ^_^
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.104.187.41
推
09/26 12:24, , 1F
09/26 12:24, 1F
※ 編輯: bobju 來自: 59.104.187.41 (09/26 20:15)
推
09/28 12:08, , 2F
09/28 12:08, 2F
→
09/28 12:50, , 3F
09/28 12:50, 3F
→
09/28 12:50, , 4F
09/28 12:50, 4F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
PHP 近期熱門文章
PTT數位生活區 即時熱門文章