<?php
require_once 'classes/template.class.php';
$options = [
'template_dir' => 'templates/',
'cache_dir' => 'templates/cache',
'auto_update' => true, // 模板變更自動更新快取
'cache_lifetime' => 1, // 快取壽命 (分鐘),0 = 永久
];
$template = Template::getInstance();
$template->setOptions($options);
// 指派變數
$testArr = ['testa' => 'a', 'testb' => 'b'];
// 載入並輸出模板
include $template->getfile('test.htm');
?>
| 語法 | 說明 |
|---|---|
{$var} | 輸出變數 |
{loop $arr $k $v}...{/loop} | 陣列迴圈 |
{if $cond}...{elseif $cond}...{else}...{/if} | 條件判斷 |
{eval ...} | 執行 PHP 代碼 |
{template header} | 引入子模板 |
cache_dir → include 執行。mtime 與快取檔 mtime,若 auto_update=true 且模板較新則重新編譯。cache_lifetime 控制快取過期;設為 0 則永不重新編譯 (效能最佳,部署時需手動清除快取)。若需 Discuz 語言包功能,請在 template.class.php 第 172 行取消註解,並在 template.func.php 加入原版 languagevar() 函式。
此封裝類保留 Discuz 模板引擎核心功能,並加入現代化快取策略,適合中小型專案快速導入模板分離架構。