1. tpl: <{* 這是註釋格式 *}>
$smarty->display("tpl.htm"); 編譯並顯示模板文件tpl.htm
<{$title|cat:"made by chenda"}> 將cat裏的值連接到給定的變量後面
<{$smarty.now|date_format:"%Y-%m-%d"}> 顯示當前日期
<{$content|default:"沒有指定文章內容"}> 當變量爲空或者未分配時,由給定的默認值替代輸出
<{$content|indent:6}> 或 <{$content|indent:2:" "}>
<{$title|replace:"teacherli":"李曉軍"}> 把變量中的teacherli替換成:李曉軍
tpl: <{html_checkboxes labels=false name="aa" options=$cust_checkboxes checked=$select_id separator="<br>"}>
php: $smarty->assign('cust_checkboxes', array(1000 => 'Joe Schmoe',1001 => 'Jack Smith')); 定義關聯數組
$smarty->assign('select_id', 1001); 指定默認值
tpl: <{html_radios name="aa" labels=false options=$cust_radios checked=$customer_id separator="<br>"}>
php: $smarty->assign('cust_radios', array(1001 => 'Joe Schmoe',1002 => 'Jack Smith',1003 => 'Jane Johnson'));
$smarty->assign('customer_id', 1001);
$tpl->assign("array1", $array1); 如:{foreach from=要循環的數組變量, item=要循環的變量名稱} { $要循環的變量名稱 } {/foreach}
<{$item1}>
<{/foreach}>
section循環: <{section name= “section的名稱,不用加$” loop= “要循環的變量,在程序中要使用assign對這個變量進行操作”}>
<{$array1[sec1]}>
<{/section}> 針對二維數組的循環: 如:
$array2 = array(
array("index1" => "data1-1", "index2" => "data1-2", "index3" => "data1-3"),
array("index1" => "data2-1", "index2" => "data2-2", "index3" => "data2-3"),
array("index1" => "data3-1", "index2" => "data3-2", "index3" => "data3-3"),
array("index1" => "data4-1", "index2" => "data4-2", "index3" => "data4-3"),
array("index1" => "data5-1", "index2" => "data5-2", "index3" => "data5-3"));
$tpl->assign("array2", $array2);
foreach 來呈現 array2 :
<{foreach item=index2 from=$array2}>
<{foreach key=key2 item=item2 from=$index2}>
<{$key2}>: <{$item2}>
<{/foreach}>
<{/foreach}>
section 來呈現 array2 :
<{section name=sec2 loop=$array2}>
index1: <{$array2[sec2].index1}>
index2: <{$array2[sec2].index2}>
index3: <{$array2[sec2].index3}>
<{/section}>
section 則以 [主數組][循環名稱].子數組索引 將整個數組呈現出來 注: section 的數組索引必須是從 0 開始的正整數,即 0, 1, 2, 3, ...如果您的數組索引不是從 0 開始的正整數,那麼就得改用 foreach 來呈現.