建立一個簡單的模組:更好的樣板

日期 2010年08月16日 22:06:52 | 新聞類別: 模組開發

原始出處 http://www.xoops.org/modules/mediawiki/index.php/Making_a_simple_module:_An_active_template


 

上個範例中我們使用了一個很簡單的樣版,現在我將介紹實際設計中利用樣版引擎功能的樣版。

跟著下面的步驟使用樣版輸出表格:

1.建立templates 目錄,在目錄下新增"myTemplate.html" 且內容如下:

<table><tr><td colspan='2' align='center'>My Module Table</td></tr>  
                      
<{section name=i loop=$myArray step=1}>
        <{if $smarty.section.i.iteration % 2 == 0}>
               <td><{$myArray[i]}></td>
               </tr>
        <{else}>
               <tr>
               <td><{$myArray[i]}></td>
        <{/if}>
<{/section}>
               
</table>

2.編輯xoops_version.php 檔案新增:

    $modversion['templates'][1]['file'] = "myTemplate.html";
    $modversion['templates'][1]['description'] = "Our template file";

3.編輯index.php 使用樣版,並將結果輸出給樣版而不是使用echo 輸出到螢幕:

<?php //index.php
        require_once("../../mainfile.php");
        
        // this line must be defined BEFORE header.php
        $xoopsOption['template_main'] = "myTemplate.html";
        include XOOPS_ROOT_PATH."/header.php";
        //Our Example Array
        $myArray = array("Richard Nixon", "American Hero", "George W. Bush", "Vegetable", "Hoven Droven", "Swedish Folk Music done cool");
        $xoopsTpl->assign('myArray', $myArray);
        include XOOPS_ROOT_PATH."/footer.php";
?>

範例中可以看到我們的陣列指定給全域的Xoops 物件xoopsTpl 的smarty 樣版變數 'myArray'。





本篇新聞來自:XOOPS Site
https://www.xoops.org.tw

本篇新聞的連結網址是:
https://www.xoops.org.tw/modules/news/article.php?storyid=43