:::

12. XOOPS的樣板檔

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

1$modversion['templates'][1]['file'] = 'tad_honor_index_tpl.html';
2$modversion['templates'][1]['description'] = 'index.php的樣板';
1$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

1$xoopsOption['template_main'] = "tad_honor_view_tpl.html";
1$modversion['templates'][2]['file'] = 'tad_honor_view_tpl.html';
2$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
    01<h1>恭賀「<{$students}>」</h1>
    02 
    03<div>
    04  恭喜 <b><{$students}></b> 於 <b><{$date}></b> 榮獲 <b><{$descript}></b>
    05 
    06  <{if $teachers}>
    07    <div>指導老師:<{$teachers}></div>
    08  <{/if}>
    09</div>
    10 
    11 
    12<{if $note}>
    13  <p><{$note}></p>
    14<{/if}>
    view.php
    1$xoopsTpl->assign( "date" , $all['honor_date']) ;
    2$xoopsTpl->assign( "students" , $all['honor_students']) ;
    3$xoopsTpl->assign( "descript" , $all['honor_descript']) ;
    4$xoopsTpl->assign( "teachers" , $all['honor_teachers']) ;
    5$xoopsTpl->assign( "note" , $all['honor_note']) ;

四、 Smarty迴圈

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

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

若語系中有如下定義:

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

 


:::

搜尋

QR Code 區塊

https%3A%2F%2Ftad0616.cp27.secserverpros.com%2Fmodules%2Ftad_book3%2Fpage.php%3Ftbdsn%3D703%26tbsn%3D25

書籍目錄

展開 | 闔起

線上使用者

76人線上 (11人在瀏覽線上書籍)

會員: 0

訪客: 76

更多…