天天看点

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>