smarty學習2

smarty模板:
1. tpl: <{* 這是註釋格式 *}>
2. tpl: <{ include file="bottom.htm"}>  將bottom.htm模板文件包含到當前模板文件中
3. php:  $smarty->assign("name","李曉軍");將模板中的$name替換成"李曉軍"三個字
  $smarty->display("tpl.htm");  編譯並顯示模板文件tpl.htm
4. tpl: <{$title|capitalize}> 首字母大寫 <{$title|lower}>  全部變小寫   <{$title|upper}>  全部變大寫
 <{$title|cat:"made by chenda"}>  將cat裏的值連接到給定的變量後面
 <{$smarty.now|date_format:"%Y-%m-%d"}> 顯示當前日期
 <{$content|default:"沒有指定文章內容"}> 當變量爲空或者未分配時,由給定的默認值替代輸出
 <{$content|indent:6}> 或 <{$content|indent:2:"&nbsp;"}>
 <{$title|replace:"teacherli":"李曉軍"}>  把變量中的teacherli替換成:李曉軍
5. <{assign var="myname" value="陳達"}> 在模板內定義一個變量     輸出該變量:<{$myname}>
6. 顯示覆選框
   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);  指定默認值
7. 顯示單選框
   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);
8.針對一維數組的循環:
foreach循環:鳥巢式的循環,一層一層往下走 $array1 array(1 => "蘋果", ="菠蘿", => "香蕉", 4 => "芭樂"); 
$tpl->assign("array1", $array1);    如:{foreach from=要循環的數組變量, item=要循環的變量名稱}                $要循環的變量名稱  }      {/foreach}
<{foreach item=item1 from=$array1}> 
 <{$item1}
<{/foreach}> 
section循環:  <{section name= “section的名稱,不用加$”        loop= “要循環的變量,在程序中要使用assign對這個變量進行操作”}>  
{$loopname[name].var}//loopname:loop處賦的名字;name:name處賦的名字;var:下標名   {/section} <{section name=sec1 loop=$array1}> 
  <{$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 :
  <{foreacitem=index2 from=$array2}> 
  <{foreackey=key2 item=itemfrom=$index2}> 
  <{$key2}>: <{$item2}> 
  <{/foreach}> 
  <{/foreach}>  
 section  來呈現 array2  :
  <{sectioname=secloop=$array2}> 
  index1: <{$array2[sec2].index1}> 
  index2: <{$array2[sec2].index2}> 
  index3: <{$array2[sec2].index3}>
 

  <{/section}> 
section 則以  [主數組][循環名稱].子數組索引  將整個數組呈現出來 注: section 的數組索引必須是從 開始的正整數,即 0, 1, 2, 3, ...如果您的數組索引不是從 0 開始的正整數,那麼就得改用 foreach 來呈現.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章