:::

12. XOOPS的樣板檔

一、 模組套用簡易樣板步驟

$modversion['templates'][1]['file'] = 'tad_honor_index_tpl.html';
$modversion['templates'][1]['description'] = 'index.php的樣板';
$xoopsOption['template_main'] = "tad_honor_index_tpl.html";
  1. 請到xoops_version.php做以下設定。設定也是兩個一組,陣列從1開始。
    • $modversion['templates'][1]['file'] = 'tad_honor_index_tpl.html';
      檔名盡量特別,別和其他樣板檔相衝。
    •  $modversion['templates'][1]['description'] = "佈景說明";
      樣板描述,從後台的樣板管理就會看得到。(常數不用加引號,不然系統讀不到)
  2. 建立templates/tad_honor_index_tpl.html,該檔內容一行即可,樣板碼可自訂:<{$樣板碼}> 例如:<{$content}>
  3. 到後台更新您的模組,讓XOOPS重讀xoops_version.php設定檔。
  4. 在index.php前面加入
  5. 用「$xoopsTpl->assign( "content" , $main) ;」來把內容套入樣板,其中 content就是樣板標籤<{$content}>,而$main就是要套用進去的內容。
  6. $xoopsTpl是在header.php產生的物件,所以,$xoopsTpl必須在header.php之後使用。若要在函數中使用,記得global $xoopsTpl;

二、 設一個獨立頁面view.php

$xoopsOption['template_main'] = "tad_honor_view_tpl.html";
$modversion['templates'][2]['file'] = 'tad_honor_view_tpl.html';
$modversion['templates'][2]['description'] = 'view.php的樣板';
  1. 將總列表和單一頁面都設在index.php,這樣雖然可以,但要套用比較精細的樣板時,難度會增加許多,因此建議增設一個獨立頁面,也就是秀出單一文章用的頁面,如此在製作樣板時會更簡單。
  2. 另存index.php為view.php,並留下顯示頁面的函數,其餘函數及流程均可刪除。另外,此頁套用新的樣板檔:
  3. 將樣板檔 tad_honor_index_tpl.html 複製為 tad_honor_view_tpl.html
  4. 到xoops_version.php多一組樣板設定:
  5. 到後台更新模組。
  6. 修改index.php中的連結至view.php,搜尋檔的$ret[$i]['link']也一樣需要修改。

三、 精細的樣板

  1. 把樣板細分成各種網頁元件,由樣板提供標籤,PHP負責產生標籤所需的套用值。
  2. 按照樣版產生流程,替view.php產生一組樣板tad_honor_view.html,其內容為: tad_honor_view.html
    <h1>恭賀「<{$students}>」</h1>
    
    <div>
      恭喜 <b><{$students}></b> 於 <b><{$date}></b> 榮獲 <b><{$descript}></b>
    
      <{if $teachers}>
        <div>指導老師:<{$teachers}></div>
      <{/if}>
    </div>
    
    
    <{if $note}>
      <p><{$note}></p>
    <{/if}>
    view.php
      $xoopsTpl->assign( "date" , $all['honor_date']) ;
      $xoopsTpl->assign( "students" , $all['honor_students']) ;
      $xoopsTpl->assign( "descript" , $all['honor_descript']) ;
      $xoopsTpl->assign( "teachers" , $all['honor_teachers']) ;
      $xoopsTpl->assign( "note" , $all['honor_note']) ;

四、 Smarty迴圈

  1. 列表部份需要用到smarty迴圈。
    tad_honor_index.html
    <table style='font-size:11pt;'>
    <{foreach item=honor from=$honor}>
      <tr>
        <td><a href='view.php?honor_sn=<{$hono.honor_sn}>'>
    <{$honor.honor_students}></a></td>
        <td><{$honor.honor_date}></td>
        <td><{$honor.honor_descript}></td>
        <td><{$honor.honor_teachers}></td>
      </tr>
    <{/foreach}>
    </table>
    <div align='center'><{$bar}></div>
    index.php
    $i=0;
    while($all=$xoopsDB->fetchArray($result)){
    //處理部份省略
      $main[$i]['honor_sn']=$all['honor_sn'];
      $main[$i]['honor_students']=$all['honor_students'];
      $main[$i]['honor_date']=$all['honor_date'];
      $main[$i]['honor_descript']=$all['honor_descript'];
      $main[$i]['honor_teachers']=$all['honor_teachers'];
      $i++;
    }
    $xoopsTpl->assign( "honor" , $main) ;
    $xoopsTpl->assign( "bar" , $bar) ;

五、在樣板中使用模組語系常數

若語系中有如下定義:

define("_MI_HONOR_YEAR" , "學年度");
那麼在樣板中就可以用這種方式呼叫該常數:
<{$smarty.const._MI_HONOR_YEAR}>

 


:::

搜尋

QR Code 區塊

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

書籍目錄

展開 | 闔起

線上使用者

44人線上 (4人在瀏覽線上書籍)

會員: 0

訪客: 44

更多…