配置文件位於 conf/comm.conf.php,核心參數如下:
$InitPHP_conf['template']['template_path'] = 'web/template'; // 模板目錄
$InitPHP_conf['template']['template_c_path'] = 'data/template_c'; // 編譯緩存目錄(需 777 權限)
$InitPHP_conf['template']['template_type'] = 'htm'; // 模板後綴
$InitPHP_conf['template']['template_c_type'] = 'tpl.php'; // 編譯後綴
$InitPHP_conf['template']['template_tag_left'] = '<!--{'; // 左標籤
$InitPHP_conf['template']['template_tag_right'] = '}-->'; // 右標籤
$InitPHP_conf['template']['is_compile'] = true; // 開發開啟,上線關閉
$InitPHP_conf['template']['driver'] = 'simple'; // 驅動:simple / default
$InitPHP_conf['template']['theme'] = ''; // 主題名稱
關鍵提示:is_compile 開發時開啟(實時編譯),生產環境關閉以提升性能;driver 決定模板語法風格。
web/template/ 存放 .htm 模板。app/data/template_c/ 並授予寫入權限。hello.htm:<h1>Hello, <!--{$name}--></h1>$this->view->display('hello');(無需後綴)。訪問後 template_c/ 下生成 hello.tpl.php,驗證編譯機制正常。
$this->view->set_tpl('test'); // 添加模板 test.htm
$this->view->set_tpl('user/userinfo'); // 支持子目錄
$this->view->set_tpl('header', 'F'); // 'F' 置頂顯示
$this->view->set_tpl('footer', 'L'); // 'L' 置底顯示
$this->view->remove_tpl('test'); // 移除已設置模板
$this->view->get_tpl(); // 獲取當前模板列表
$this->view->display(); // 輸出渲染
<!--{echo $uid;}-->
<!--{if (true == true) { }-->
<!--{}}-->
<!--{layout:user}-->
<!--{$username}--> // 輸出變量
<!--{APP_PATH}--> // 輸出常量
<!--{if ($a == 'yes')}-->...<!--{/if}-->
<!--{foreach ($a as $k => $v)}-->...<!--{/foreach}-->
<!--{for ($i=0; $i<100; $i++)}-->...<!--{/for}-->
<!--{layout:user}--> // 佈局引入
initphp/core/view/driver/,命名 {driver}.init.php,類名 {driver}Init。public function init($str, $left, $right),通過正則替換標籤。$InitPHP_conf['template']['driver'] = 'your_driver'; 啟用。在模板中寫 <!--{layout:version}--> 即可引入 version.htm,支持子目錄 user/version。解決頁面公共區塊復用。
設置 $InitPHP_conf['template']['theme'] = 'red';,模板自動從 template/red/ 讀取,實現無縫切換皮膚。
模板編譯為 PHP 文件存放於 template_c/。開發階段 is_compile=true 保證即時生效;上線後設為 false 避免重複編譯開銷。
InitPHP 模板引擎輕量易用,Simple 驅動語法清晰,配合 Layout、Theme、自定義標籤等特性,能滿足中小型項目的視圖層需求。合理配置編譯緩存是性能優化關鍵。