天天看點

ecmall widgets 挂件開發詳解

Ecmall挂件開發

實質上是背景開發很多頁面,分别去調用程式展示這些頁面,達到首頁内容更換很快的目的,這樣做減少後續開發,開發人員隻需開發挂件就可以了,至于位置可随意定.(還需調整html,但是起碼背景取資料不用做了)

流程介紹:

1:ecmall模闆頁面調用widget頁面(整個過程比較複雜)

  <!--{widgets page=index area=cycle_image}-->

參數:page:指明頁面是index頁面

     Area:指明顯示的區域。(相當于告訴程式生成的頁面是放在那裡的)

2:經過ecmall模闆引擎重新生成一個臨時php檔案,上面那句代碼被解析成這樣的php代碼。

<!--{widgets page=index area=cycle_image}-->

                     ||

<?php $this->display_widgets(array('page'=>'index','area'=>'cycle_image')); ?>

3:檢視下display_widgets()方法的源碼

/**

* 視圖回調函數[顯示小挂件]

*

* @author    Garbin

* @param     array $options

* @return    void

*/

function display_widgets($options) {

$area = isset ( $options ['area'] ) ? $options ['area'] : '';

$page = isset ( $options ['page'] ) ? $options ['page'] : '';

if (! $area || ! $page) {

return;

}

include_once (ROOT_PATH . '/includes/widget.base.php');

/* 擷取該頁面的挂件配置資訊 */

$widgets = get_widget_config ( $this->_get_template_name (), $page );

/* 如果沒有該區域 */

if (! isset ( $widgets ['config'] [$area] )) {

/*将該區域内的挂件依次顯示出來 */

foreach ( $widgets ['config'] [$area] as $widget_id ) {

$widget_info = $widgets ['widgets'] [$widget_id];

$wn = $widget_info ['name'];

$options = $widget_info ['options'];

$widget = & widget ( $widget_id, $wn, $options );

$widget->display ();

* 擷取目前使用的模闆名稱

* @return    string

function _get_template_name() {

return 'default';

*    擷取指定風格,指定頁面的挂件的配置資訊

*    @author    Garbin

*    @param     string $template_name

*    @param     string $page

*    @return    array

function get_widget_config($template_name, $page)//default index

{

    static $widgets = null;

    $key = $template_name . '_' . $page;

    if (!isset($widgets[$key]))

    {

        $tmp = array('widgets' => array(), 'config' => array());

        $config_file = ROOT_PATH . '/data/page_config/' . $template_name . '.' . $page . '.config.php';

        if (is_file($config_file))

        {

            /* 有配置檔案,則從配置檔案中取 */

            $tmp = include_once($config_file);

        }

        $widgets[$key] = $tmp;

    }

    return $widgets[$key];

*    擷取挂件執行個體

*    @param     string $id

*    @param     string $name

*    @param     array  $options

*    @return    Object Widget

function &widget($id, $name, $options = array())

    if (!isset($widgets[$id]))

        $widget_class_path = ROOT_PATH . '/external/widgets/' . $name . '/main.widget.php';

        $widget_class_name = ucfirst($name) . 'Widget';

        include_once($widget_class_path);

        $widgets[$id] = new $widget_class_name($id, $options);

    return $widgets[$id];

    /**

     *    顯示

     *

     *    @author    Garbin

     *    @param    none

     *    @return    void

     */

    function display()

        echo $this->get_contents();

     *    将取得的資料按模闆的樣式輸出

     *    @return    string

    function get_contents()

        /* 擷取挂件資料 */

        $this->assign('widget_data', $this->_get_data());

        /*可能有問題*/

        $this->assign('options', $this->options);

        $this->assign('widget_root', $this->widget_root);

        return $this->_wrap_contents($this->fetch('widget'));

執行個體開發:

1:在頁面上添加要展示的頁面子產品

<div class="left" area="bottom_foot" widget_type="area">

    <!--{widgets page=index area=bottom_foot}-->

</div>

2:修改工程目錄下/data/page_config/default.index.config.php添加該子產品的相關資訊

   'widgets' => 

  array (

     '_widget_1000' => 

                 array (

                 'name' => 'test',

                 'options' => 

                             array (

                             'ad_image_url' => 'data/files/mall/template/200908070207084061.gif',

                             'ad_link_url' => '',

                              ),

                 ),

  ),

  'config' => 

    array(

      'bottom_foot' => 

      array (

            0 => '_widget_1000',

            ),

),

3:在工程目錄external/widgets建name(跟上面定義的name要一緻)目錄,然後再建檔案main.widget.php  

  class TestWidget extends BaseWidget{

    var $_name = 'test';

    function _get_data(){

      $test_mod=&m('test');

      $users=$test_mod->getAll("select * from ecm_member");

          return $users;

  }  

4:在includes/model下模組化型檔案(同資料庫互動)

  class TestModel extends BaseModel{

   }

5:在同級目錄建立widget.html檔案(該模闆為展示内容)

<div class="module_common">

    <h2><b class="news" title="NEWS公告欄"></b></h2>

    <div class="wrap">

        <div class="wrap_child">

            <ul class="news_list">

                <!--{foreach from=$widget_data item=user}-->

                <li>{$user[user_name]}</li>

                <!--{/foreach}-->

            </ul>

        </div>

    </div>