class.fasttemplate.php)。頁面 = 大區塊組合,區塊 = 子區塊組合,最小單元 = 字串變數。模板中以 {VAR_NAME} 佔位,解析時由內而外替換。
$tpl = new FastTemplate("template_dir/");$tpl->define(array("main" => "main.tpl", "row" => "row.tpl"));$tpl->assign("VAR_NAME", "值");$tpl->parse("OUTPUT_HANDLE", "BLOCK_HANDLE");$tpl->FastPrint("OUTPUT_HANDLE");<?php
require_once "class.fasttemplate.php";
$tpl = new FastTemplate("templates/");
$tpl->define(array(
"page" => "page.tpl",
"header" => "header.tpl",
"footer" => "footer.tpl",
"row" => "row.tpl"
));
// 指派全域變數
$tpl->assign("SITE_TITLE", "My FastTemplate Site");
$tpl->assign("YEAR", date("Y"));
// 解析 header/footer 到 page
$tpl->parse("HEADER", "header");
$tpl->parse("FOOTER", "footer");
// 迴圈產生列表
$items = array("Apple", "Banana", "Cherry");
$list = "";
foreach ($items as $item) {
$tpl->assign("ITEM_NAME", $item);
$tpl->parse("LIST_ROW", "row", true); // true = append
$list = $tpl->get("LIST_ROW"); // 取得累加結果
}
$tpl->assign("CONTENT_LIST", $list);
// 最終解析並輸出
$tpl->parse("PAGE", "page");
$tpl->FastPrint("PAGE");
?>
<!-- page.tpl -->
<html><head><title>{SITE_TITLE}</title></head><body>
{HEADER}
<ul>{CONTENT_LIST}</ul>
{FOOTER}
</body></html>
<!-- header.tpl -->
<h1>Welcome to {SITE_TITLE}</h1>
<!-- row.tpl -->
<li>{ITEM_NAME}</li>
<!-- footer.tpl -->
<hr>© {YEAR} {SITE_TITLE}
| 優點 | 缺點 |
|---|---|
| 極輕量、單檔、無依賴 | 功能較簡單,無快取、無插件機制 |
| 語法直觀,學習曲線平緩 | 大型專案維護性不如 Smarty/PHPLib |
| 適合中小專案、快速原型 | 社群較小,更新較慢 |