ecmall widgets 挂件开发详解

小编 2026-06-16 阅读:772 评论:0
Ecmall挂件开发实质上是后台开发很多页面,分别去调用程序展示这些页面,达到首页内容更换很快的...
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>
版权声明

本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。

热门文章
  • 机房智能化温湿度解决方式之POE供电以太网温湿度传感器

    机房智能化温湿度解决方式之POE供电以太网温湿度传感器
    机房智能化温湿度解决方式之POE供电以太网温湿度传感器 北京盈创力和电子科技有限公司 智能型TCP网口温湿度记录仪 北京IP网络温湿度记录仪厂家,北京盈创力和 北京智能型TCP网口温湿度记录仪IP网络温湿度记录仪是一种新型的基于TCP/IP协议双绞线以太网标准温湿度采集模块,利用它可以实现现场温度值、相对湿度值的采集,同时利用其自身的RJ45通信接口可以方便地和机房监控主机或交换机集线器进行联网。 工作于-40℃~85℃工业级带...
  • Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering

    Sequential Monte Carlo Methods (SMC) 序列蒙特卡洛/粒子滤波/Bootstrap Filtering
    Problem Statement 我们考虑一个具有马尔可夫性质、非线性、非高斯的状态空间模型(State Space Model):对于一个时间序列上的观测结果{yt,t∈N}\\{ y_t , t \\in N \\}{yt​,t∈N},我们认为每个观测结果yty_tyt​的生成依赖于一个无法直接观察的隐变量xt∈{xt,t∈N}x_t \\in \\{x_t , t \\in N \\}xt​∈{xt​,t∈N},即:p(...
  • HTTP状态保持的原理

    HTTP状态保持的原理
    a)在用户登录之后,浏览器返回响应的时候会在响应中添加上cookieb)浏览器接收到cookie之后会自动保存c)当用户再次请求同一服务器中的其他网页的时候,浏览器会自动带上之前保存的cookied)服务接收到请求之后可以请 request 对象中取到cookie 判断当前用户是否登录  Http是无状态的,就是连接时数据互通,关闭后...
  • Hive 系统函数及示例

    Hive 系统函数及示例
    查看所有系统函数 show functions; 函数分类 内置函数【系统函数】 数学函数: floor、round、ceil、cos、log2等 字符串函数: length、reverse、trim、lower、get_json_object、repeat等 收集函数: size 转换函数: cast 日期函数: year、month、datediff、date、date_add等 条件函数: coalesce、case…w...
  • CSRF的原理和防范措施

    CSRF的原理和防范措施
    a)攻击原理:i.用户C访问正常网站A时进行登录,浏览器保存A的cookieii.用户C再访问攻击网站B,网站B上有某个隐藏的链接或者图片标签会自动请求网站A的URL地址,例如表单提交,传指定的参数iii.而攻击网站B在访问网站A的时候,浏览器会自动带上网站A的cookieiv.所以网站A在接收到请求之后可判断当前用户是登录状态,所以...
标签列表