:::

6-5 陣列傳至Smarty樣板的用法

傳送內容 PHP檔(*.php) Smarty樣板檔(*.tpl)
一般變數
$action="活動名稱";
$xoopsTpl->assign('action', $action);

 

<{$action}>

 

一維陣列
$action['title']="活動名稱";
$action['content']="活動內容";
$smarty->assign('action', $action);

 

<h1><{$action.title}></h1>
<{$action.content}>

 

二維陣列
$actions[0]['title']="活動1名稱"; 
$actions[0]['content']="活動1內容";
$actions[1]['title']="活動2名稱"; 
$actions[1]['content']="活動2內容";
$smarty->assign('actions', $actions);

 

<{foreach from=$actions item=action}>
  <h1><{$action.title}></h1>
  <{$action.content}>
<{/foreach}>

或

<h1><{$actions.0.title}></h1>
<{$actions.0.content}>
<h1><{$actions.1.title}></h1>
<{$actions.1.content}>

 

XOOPS中Smarty迴圈相關用法

  1. Smarty迴圈用來處理陣列,常用方法如下:
    <{foreach from=$來源變數 item=$別名 name=迴圈別名}>
      <{$別名.索引}>
    <{foreachelse}>
      該變數沒有值時要出現的內容
    <{/foreach}>
  2. 迴圈還有一些特別的用法:
    • <{$smarty.foreach.迴圈別名.first}> 迴圈第一圈
    • <{$smarty.foreach.迴圈別名.last}> 迴圈最後一圈
    • <{$smarty.foreach.迴圈別名.index}> 取得迴圈的索引值,依序輸出0、1、2......
    • <{$smarty.foreach.迴圈別名.iteration}> 取得迴圈的計數值,依序輸出1、2、3......
    • <{$smarty.foreach.迴圈別名.total}> 取得迴圈執行總數
  3. 詳情可見:https://www.smarty.net/docsv2/en/language.function.foreach.tpl

:::

書籍目錄

展開 | 闔起

http%3A%2F%2Fcampus-xoops.tn.edu.tw%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D29%26tbdsn%3D883

計數器

今天: 793793793
昨天: 3438343834383438
總計: 7391122739112273911227391122739112273911227391122