如何在Codeigniter中创建窗口小部件系统 [英] How to create a widget system in Codeigniter

查看:135
本文介绍了如何在Codeigniter中创建窗口小部件系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Codeigniter中创建一个自定义CMS,我想要一个类似于Wordpress中使用的widget系统。

I am creating a custom CMS in Codeigniter and I'd like to have a widget system similar to what is used in Wordpress.

例如,喜欢有一个小部件显示最后5个帖子显示在侧边栏。

For example, I'd like to have a widget that shows the last 5 posts displayed on the sidebar. I'd also like to be able to control what pages this widget appears on page-by-page basis.

我使用的是 Phil Sturgeon的模板库,因此示例控制器如下所示:

I am using Phil Sturgeon's Template library, so an example controller looks like:

$this->template->set_partial('header', 'layouts/header');
$this->template->set_partial('footer', 'layouts/footer');
$this->template->set_partial('sidebar', 'layouts/sidebar');

$this->data['title'] = "Create Post";
$this->template->build('create', $this->data);

我想坚持使用MVC模式,所以我不想把逻辑侧边栏视图,这是我现在可以想到的唯一的东西。

I'd like to stick with the MVC pattern, so I don't want to put logic in the sidebar view, which is the only thing I can think of right now.

我应该为此使用HMVC吗?

Is HMVC something I should be using for this?

如何告诉侧边栏哪些小部件要显示?

How can I tell the sidebar which widgets to display?

推荐答案

以下是 Wiredesignz

了解详情

/**
 * Widget Plugin 
 * 
 * Install this file as application/plugins/widget_pi.php
 * 
 * @version:     0.21
 * $copyright     Copyright (c) Wiredesignz 2009-09-07
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
class Widget
{
    public $module_path;

    function run($file) {        
        $args = func_get_args();

        $module = '';

        /* is module in filename? */
        if (($pos = strrpos($file, '/')) !== FALSE) {
            $module = substr($file, 0, $pos);
            $file = substr($file, $pos + 1);
        }

        list($path, $file) = Modules::find($file, $module, 'widgets/');

        if ($path === FALSE) {
            $path = APPPATH.'widgets/';
        }

        Modules::load_file($file, $path);

        $file = ucfirst($file);
        $widget = new $file();

        $widget->module_path = $path;

        return call_user_func_array(array($widget, 'run'), array_slice($args, 1));    
    }

    function render($view, $data = array()) {
        extract($data);
        include $this->module_path.'views/'.$view.EXT;
    }

    function load($object) {
        $this->$object = load_class(ucfirst($object));
    }

    function __get($var) {
        global $CI;
        return $CI->$var;
    }
} 






示例




Example

// application/widgets/Hello_world.php
class Hello_world extends Widget
{
    function run() {
        $this->render('hello_world');
    }
} 

在视图中调用静态run小部件类:

In your view call the static "run" method on the widget class:

widget::run('hello_world');

这篇关于如何在Codeigniter中创建窗口小部件系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆