Smarty、Dwoo 等模板引擎默認使用 { } 作為界定符,而 CSS 選擇器、JS 對象字面量、JSON 同樣大量使用大括號,導致模板解析錯誤或輸出錯亂。
將 CSS/JS 寫入獨立文件,頁面通過 <link>/<script src> 引入。
// Smarty
$smarty->left_delimiter = '{{';
$smarty->right_delimiter = '}}';
// Dwoo
$dwoo->setDelimiters('{{', '}}');
{literal} 標籤包裹(推薦·綠色無侵入)在衝突區塊用 {literal}...{/literal} 包裹,引擎將內容原樣輸出,不做解析。
<style type="text/css">
{literal}
.container { margin: 2px; padding: 0; }
.box:hover { background: #f00; }
{/literal}
</style>
<script type="text/javascript">
function openDialog(id) {
{literal}
art.dialog.open(url, {
id: 'select',
title: '選擇',
width: 760,
height: 380,
padding: '10px'
});
{/literal}
}
</script>
<script>
const config = {literal}{"apiUrl": "/api/v1", "token": "abc123"}{/literal};
</script>
Dwoo 繼承了 Smarty 的 {literal} 語法,用法完全一致,無需額外配置。
{{ }}),從源頭消除衝突{literal},不影響現有邏輯data-* 屬性或單獨 API 接口傳遞,模板只輸出靜態骨架{literal} 區塊內語法高亮,提升開發體驗{literal} 是解決歷史項目模板衝突的「銀彈」——零配置、零重構、零風險,精準修復衝突點,是維護期項目的首選方案。