capture的作用是: 捕獲模板輸出的數據並將其存儲到一個變數,而不是把它們輸出到頁面,任何在 {capture name="foo"}和{/capture}之間的數據將被存儲到變數$foo中,該變數由name屬性指定,在模板中通過 $smarty.capture.foo 訪問該變數,{captu ...
capture的作用是:
捕獲模板輸出的數據並將其存儲到一個變數,而不是把它們輸出到頁面,任何在 {capture name="foo"}和{/capture}之間的數據將被存儲到變數$foo中,該變數由name屬性指定,在模板中通過 $smarty.capture.foo 訪問該變數,{capture}必須成對出現,即以{/capture}作為結尾,該函數不能嵌套使用,capture有兩個屬性:name和assignassign;
實例:
第一種情況:使用name屬性
{capture name='color'}red{/capture}
{$smarty.capture.color}
第二種情況:使用assign,捕獲內容到變數
{capture assign='foo'}blue{/capture}
{$foo}
第三種情況:使用append,捕獲內容到數組
{capture append='arr'}abs{/capture}
{capture append='arr'}zks{/capture}
{foreach $arr as $val}{$val}<br>{/foreach}