{capture name="sidebar"}
<div class="widget">...複雜內容...</div>
{/capture}
<div class="layout">
{$smarty.capture.sidebar}
</div>
用途:重複使用區塊、條件式輸出、傳遞給變數再處理。
configs/test.conf:
pageTitle = "設定檔測試"
bodyBgColor = "#f0f0f0"
img = "logo.png"
index.tpl:
{config_load file="test.conf"}
<html><head><title>{#pageTitle#}</title></head>
<body bgcolor="{#bodyBgColor#}">
<img src="{#img#}">
注意:設定檔預設放在 configs/ 目錄,變數以 {#var#} 輸出。
{php}
global $db;
$res = $db->query("SELECT * FROM news LIMIT 5");
while ($row = $res->fetch()) {
echo "<li>{$row['title']}</li>";
}
{/php}
⚠️ 破壞模板與邏輯分離,僅限緊急臨時方案。
{strip}
<div>
<ul>
<li>項目 1</li>
<li>項目 2</li>
</ul>
</div>
{/strip}
{* 輸出:- 項目 1
- 項目 2
*}
減少傳輸量,加快瀏覽器渲染。
{fetch file="./data/announcement.txt" assign="ann"}
{if $ann}
<div class="announcement">{$ann}</div>
{/if}
類似 file_get_contents,可指定 assign 存入變數。
{* PHP: define('SITE_NAME', '我的網站'); *}
<h1>{$smarty.const.SITE_NAME}</h1>
無需在 PHP 端 assign,模板直接存取所有已定義常數。