Smarty 是 PHP 中最經典的模板引擎之一,透過將邏輯與表現分離,大幅提升代碼可維護性。所有 Smarty 標籤皆包裹於定界符內(預設為 {}),定界符外的內容視為靜態輸出。開發者可自訂定界符,例如:
$smarty->left_delimiter = '<!--{';
$smarty->right_delimiter = '}-->';
模板註釋以 {* ... *} 包裹,不會輸出至前端,僅供模板內部參考。
$ 開頭,支援字母、數字、底線組合。#var# 格式引用。Smarty 標籤可輸出變量或調用函數,語法統一為 {funcname attr1="val" attr2="val"}。內建函數(如 {if}、{section}、{strip})不可修改;自定義函數透過插件機制擴充,靈活度極高(如 {html_options}、{html_select_date})。
僅識別英數字、底線及 [] 組合;複雜變量需用反引號包裹,如 {func var="test `$foo.bar` test"}。
需輸出定界符本身時,可用 {ldelim}/{rdelim}、{$smarty.ldelim}/{$smarty.rdelim} 或置於 {literal}...{/literal} 區塊中。
{$smarty}{$smarty} 提供對特殊模板變量的存取:
{$smarty.get}、{$smarty.post}、{$smarty.cookies}、{$smarty.server}、{$smarty.env}、{$smarty.session}、{$smarty.request}(綜合以上)。{$smarty.now} 可搭配 date_format 修飾符格式化輸出。{$smarty.const.CONST_NAME}。{$smarty.capture.name} 對應 {capture name="name"}...{/capture}。{$smarty.config.var} 等同 {#var#}。{$smarty.section.name}、{$smarty.foreach.name}。{$smarty.template}(當前模板名)、{$smarty.version}(版本號)、{$smarty.ldelim}/{$smarty.rdelim}(定界符)。修飾符以 | 套用,參數以 : 分隔。針對陣列變量時,預設逐元素處理;若需整體處理,在修飾符前加 @(如 {$arr|@count})。常用修飾符包括:
| 修飾符 | 功能 | 參數說明 |
|---|---|---|
| capitalize | 單字首字大寫 | 布林值決定含數字單字是否大寫,預設否 |
| count_characters | 計算字元數 | 布林值決定是否計算空格,預設否 |
| cat | 字串連接 | 參數為欲連接字串,預設空字串 |
| count_paragraphs | 計算段落數 | 無 |
| count_sentences | 計算句子數 | 無 |
| count_words | 計算單字數 | 無 |
| date_format | 日期格式化 | 第一參數為格式字串(支援 strftime 格式),第二參數為預設時間 |
| default | 預設值 | 變量為空或未賦值時輸出指定值 |
| escape | 編碼轉義 | html、htmlall、url、quotes、hex、hexentity、javascript,預設 html |
| indent | 縮排 | 第一參數縮排字元數(預設4),第二參數縮排字元(預設空格) |
| lower | 轉小寫 | 無 |
| nl2br | 換行轉 <br /> | 無 |
| regex_replace | 正則替換 | 參數1:正則模式,參數2:替換字串 |
| replace | 簡單搜尋替換 | 參數1:被替換字串,參數2:替換字串 |
| spacify | 字元間插入字串 | 參數為插入字串,預設空格 |
| string_format | 格式化浮點數 | sprintf 格式字串,如 %.2f |
| strip | 移除多餘空白 | 參數為替換字串,預設單一空格 |
| strip_tags | 移除 HTML 標籤 | 無 |
| truncate | 截斷字串 | 參數1:長度(預設80),參數2:尾綴(預設...),參數3:是否精確截斷單字(預設false) |
| upper | 轉大寫 | 無 |
| wordwrap | 行寬限制 | 參數1:寬度(預設80),參數2:斷行字元(預設\n),參數3:是否強制斷字(預設false) |
將區塊輸出存入變量而非直接顯示:{capture name="foo"}...{/capture},讀取用 {$foo} 或 {$smarty.capture.foo}。不可巢狀使用。
必填 file 指定檔名,section 指定區段,scope 設定作用域(local/parent/global,預設 local)。
簡易版 section,必填 from(來源陣列)、item(當前元素變量名),可選 key(鍵名)、name(迴圈名供屬性存取)。foreachelse 在陣列為空時執行。
必填 file 指定模板,assign 將輸出存入變量。可自訂屬性傳遞區域變量。
file 指定路徑,once 控制是否僅載入一次(預設 true)。
name 指定函數名(實際調用 insert_name()),assign 存入變量,script 指定腳本路徑。結果不緩存,適合廣告、投票等動態內容。
語法近似 PHP,運算子間需以空格分隔。新增運算子:eq、ne/neq、gt、lt、gte/ge、lte/le、not、mod、is [not] div by、is [not] even、is [not] odd。
{php}...{/php} 允許模板中直接撰寫 PHP,慎用。
必填 name(巢狀識別)、loop(迴圈次數)。支援 start、step、max、show 等屬性。迴圈屬性透過 {$smarty.section.name.var} 存取(index、iteration、first、last、total 等)。
{strip}...{/strip} 區塊內的重複空白、換行、Tab 將被壓縮。
var、value。name)、初始值(start)、步長(skip)、方向(direction)、輸出控制(print)、存入變量(assign)。values 為值列表,delimiter 分隔符(預設逗號)。output 指定格式(html/javascript)。var 指定變量,assign 存入結果。file 指定路徑,assign 存入變量。name、values、output、selected、options、separator、labels。basedir、href 等屬性。equation 定義運算式(支援 + - * / abs ceil cos exp floor log log10 max min pi pow rand round sin sqrt tan 等),var 賦值變量,format 格式化結果,assign 存入變量。encode 選擇 none/hex/javascript。| 常量/變量 | 說明 | 預設值 |
|---|---|---|
| SMARTY_DIR | Smarty 類別目錄完整路徑(須以 / 結尾) | 自動偵測 |
| SMARTY_CORE_DIR | 核心檔案目錄 | SMARTY_DIR/core/ |
| $template_dir | 模板目錄 | ./templates |
| $compile_dir | 編譯目錄 | ./templates_c |
| $config_dir | 配置檔目錄 | ./configs |
| $plugins_dir | 插件目錄 | plugins |
| $debugging | 除錯主控台開關 | false |
| $debug_tpl | 除錯模板 | - |
| $debugging_ctrl | 除錯控制方式 | - |
| $autoload_filters | 全域過濾器陣列 | - |
| $compile_check | 每次執行檢查模板變更 | true |
| $caching | 啟用緩存 | false |
| $cache_dir | 緩存目錄 | ./cache |
| $cache_lifetime | 緩存壽命(秒),-1 為永久 | 3600 |
<?php
error_reporting(7);
$TurnDot = substr(PHP_OS, 0, 3) == 'WIN' ? ';' : ':';
$doc_root = $_SERVER['DOCUMENT_ROOT'];
$lib_root = "." . $TurnDot . $doc_root . "/shopadmin" . $TurnDot . $doc_root . "/shopadmin/inc" . $TurnDot . $doc_root . "/" . $TurnDot . $doc_root . "/jpgraph/src/" . $TurnDot . $doc_root . "/Smarty/libs/" . $TurnDot . $doc_root . "/Smarty";
ini_set("include_path", $lib_root);
include("conf.global.php");
$Templates_root = $doc_root . "/templates";
define("Templates", $Templates_root);
$templates = isset($INFO['templates']) ? $INFO['templates'] : 'default';
include_once('Smarty.class.php');
$tpl = new Smarty();
$tpl->debugging = false;
$tpl->template_dir = Templates . "/" . $templates;
$tpl->compile_dir = Templates . "/" . $templates . "/templates_c";
$tpl->cache_dir = $doc_root . "/cache";
$tpl->cache_lifetime = 60 * 60 * 24;
$tpl->cache_lifetime = 0; // 開發階段關閉緩存
$tpl->caching = false; // 上線前改為 true
$tpl->left_delimiter = '<{';
$tpl->right_delimiter = '}>';
$tpl->assign("template_dir", $INFO['site_url'] . "/templates/" . $templates);
$tpl->assign("Site_Url", $INFO['site_url']);
$tpl->assign("LanguageIs", $INFO['IS']);
$tpl->assign("HtmlTitle", $INFO['site_title']);
$tpl->assign("HtmlMeta", $INFO['meta_keyword']);
?>