Smarty 3 採用模組化設計:
libs/Smarty.class.php - 核心類別libs/sysplugins/ - 內建外掛plugins/ - 自訂外掛擴充目錄templates/ - 模板檔案,支援傳統語法與純 PHP<?php
require('Smarty.class.php');
$smarty = new Smarty();
$smarty->assign('foo', 'bar');
$smarty->display('index.tpl');
Smarty 3 新增 Singleton 存取:
$smarty = Smarty::instance();
Smarty 3 最大亮點是直接支援 PHP 語法模板,不再強制使用專屬標籤:
<?php
$smarty->display('php:mytemplate.tpl');
模板中可直接使用 <?=$foo?> 等原生語法。這對成熟團隊極具吸引力,但安全性需自行把控。
$smarty->display('string:This is my template, {$foo}!');
直接渲染字串模板,適合動態生成郵件內容、API 回應等場景。
Smarty 3 保持向後相容,現有專案可平滑升級。新專案建議評估:若團隊偏好原生 PHP 模板,Smarty 3 提供最佳平衡;若需嚴格分離邏輯與視圖,傳統語法依然適用。