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] )) {
return;
}
 
/*將該區域內的掛件依次顯示出來 */
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 ();
}
}
 
/**
* 獲取當前使用的模板名稱
*
* @author    Garbin
* @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];
}
 
 
/**
*    獲取掛件實例
*
*    @author    Garbin
*    @param     string $id
*    @param     string $name
*    @param     array  $options
*    @return    Object Widget
*/
function &widget($id, $name, $options = array())
{
    static $widgets = null;
    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();
}
 
    /**
     *    將取得的數據按模板的樣式輸出
     *
     *    @author    Garbin
     *    @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>
</div>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM