:::

2-2-3 關於 $xoopsTpl 樣板物件

您沒有觀看影片的權限

您沒有觀看影片的權限

請先登入,登入後,確認您的權限後,即可觀看影片。

  1. XOOPS的樣板引擎是使用 smarty 2.6.x(所以,新的 smarty 3.x 語法無法使用)
  2. 頁面的主樣板檔用 $xoopsOption['template_main'] 來指定,需註冊到 xoops_version.php 中的 $modversion['templates'] 中。
  3. 樣板檔副檔名可為 .html .tpl(建議)
  4. XOOPS的樣板引擎物件為 $xoopsTpl,在php檔中套用變數到樣板的語法為:
    $xoopsTpl->assign('樣板變數名稱' , '變數值');

     

  5. 在樣板中,則是使用以下方式來呈現變數:
    <{$樣板變數名稱}>

     

  6. 如果是陣列,則用 . 來顯示,例如:
    <{$陣列.索引}>

     

  7. Smarty保留字(https://www.smarty.net/docs/en/language.variables.smarty.tpl),常用的以下這些:
    1. 可以顯示常數
      <{$smarty.const.常數}>

       

    2. 可以顯示各種超級全域變數
      <{$smarty.get.變數}>
      <{$smarty.post.變數}>
      <{$smarty.session.變數}>
      <{$smarty.cookie.變數}>
      <{$smarty.server.變數}>

       

    3. 還可以抓取目前時間(這是時間戳記)
      <{$smarty.now}>

       

  8. XOOPS中的Smarty註解
    <{* 這裡面的東西不會被編譯 *}>

     

  9. 可以套用PHP的函數(strlen是算文字長度)
    <{$title|strlen}>

    也可以加參數(標題只截出前面數來30個字)

    <{$title|mb_substr:0:30}>
  10. 可以在樣板中設定新變數
    <{assign var=名稱 value=值}>

     

  11. 可把流程中的每個需顯示的op動作,獨立做成樣板檔,然後自動引入($xoops_rootpath 就是實際張裝路徑,不用改)
    <{includeq file="$xoops_rootpath/modules/模組名稱/templates/op_`$op`.tpl"}>

     

  12. 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}>

     

  13. Smarty迴圈用來處理陣列,常用方法如下(二維索引):
    <{foreach from=$來源變數 item=樣板變數 name=迴圈別名}>
        <{$樣板變數.索引}>
    <{foreachelse}>
        該變數沒有值時要出現的內容
    <{/foreach}>

     

  14. 迴圈還有一些特別的用法:
    • <{$smarty.foreach.迴圈別名.first}> 迴圈第一圈
    • <{$smarty.foreach.迴圈別名.last}> 迴圈最後一圈
    • <{$smarty.foreach.迴圈別名.index}> 取得迴圈的索引值,依序輸出0、1、2......
    • <{$smarty.foreach.迴圈別名.iteration}> 取得迴圈的計數值,依序輸出1、2、3......
    • <{$smarty.foreach.迴圈別名.total}> 取得迴圈執行總數
  15. 詳情可見:https://www.smarty.net/docsv2/en/language.function.foreach.tpl
  16. 也可以用判斷式
    <{if $smarty.now > $end_time}>
        活動已結束
    <{elseif $smarty.now > $start_time}>
        活動開始
    <{else}>
        活動尚未開始
    <{/if}>

     


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.net%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbsn%3D48%26tbdsn%3D1583

書籍目錄

展開 | 闔起

線上使用者

126人線上 (20人在瀏覽線上書籍)

會員: 0

訪客: 126

更多…