ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Widget plugin (intelligent view partials)

March 24, 2009 12:29am

Subscribe [34]
  • #16 / May 27, 2009 5:45pm

    Thanks wiredesignz for sharing this code.  I have done something similar with a lesser compact/elegant syntax to invoke them.  In these approaches, I am not too happy w/ the following:

    1) typically in a MVC world, the controller executes the biz logic and invokes the template to render.  In thus case the controller renders the template, then the template invokes the widgets which in turn calls the associated its biz logic.  The interaction is somehow doesn’t feel natural to me

    2) passing parameters into the widgets is not very practical.  The controllers needs to pass the params into the template and pass back to the widget.

    BTW, this may be obvious, but I didn’t understand as what would trigger the call to the render() method of the widget in your case?

    Regards

  • #17 / May 27, 2009 9:45pm

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Widgets display view partials only. They are not designed to process “biz” logic. Hence parameters are pushed to widgets from the view.

    @victorlenus, Load the model from it’s page controller and pass it to the widget via the view.

  • #18 / Aug 21, 2009 12:43pm

    Mikle

    30 posts

    wiredesignz, can you convert your widgets code to php 5?

  • #19 / Aug 22, 2009 1:32am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Widget plugin PHP5 only

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    /**
    * Widget Plugin
    *
    * Install this file as application/plugins/widget_pi.php
    *
    * @version:     0.2
    * $copyright    Copyright (c) Wiredesignz 2009-08-24
    *
    * 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
    {
        function run($name) {        
            $args = func_get_args();
            
            require_once APPPATH.'widgets/'.$name.EXT;
            $name = ucfirst($name);
            
            $widget =& new $name();
            return call_user_func_array(array(&$widget, 'run'), array_slice($args, 1));    
        }
        
        function render($view, $data = array()) {
            extract($data);
            include APPPATH.'widgets/views/'.$view.EXT;
        }
    
        function load($object) {
            $this->$object =& load_class(ucfirst($object));
        }
    
        function __get($var) {
            static $ci;
            isset($ci) OR $ci = get_instance();
            return $ci->$var;
        }
    }
  • #20 / Aug 22, 2009 5:37am

    Mikle

    30 posts

    Widget plugin PHP5 only

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    /**
    * Widget Plugin
    *
    * Install this file as application/plugins/widget_pi.php
    *
    * @version:     0.2
    * $copyright    Copyright (c) Wiredesignz 2009-08-24
    *
    * 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
    {
        function run($name) {        
            $args = func_get_args();
            
            require_once APPPATH.'widgets/'.$name.EXT;
            $name = ucfirst($name);
            
            $widget =& new $name();
            return call_user_func_array(array(&$widget, 'run'), array_slice($args, 1));    
        }
        
        function render($view, $data = array()) {
            extract($data);
            include APPPATH.'widgets/views/'.$view.EXT;
        }
    
        function load($object) {
            $this->$object =& load_class(ucfirst($object));
        }
    
        function __get($var) {
            static $ci;
            isset($ci) OR $ci = get_instance();
            return $ci->$var;
        }
    }

    Thank you for php 5 ver!

    Functions of widget class must be public or private?

  • #21 / Aug 22, 2009 5:43am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    @Mikle, You make me work so hard these days. 😊

    Any method not declared specifically is assumed to be public method.

    Widget class does not need private method.

  • #22 / Aug 22, 2009 5:45am

    Mikle

    30 posts

    Wiredesignz, how do you think wigets must have config file (like Wordpress, for activation deactivation) or not? Or I should store config in db?

  • #23 / Aug 22, 2009 5:48am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Widget config should maybe be part of config/application.php

    I always have a dedicated application config file, this keeps all site settings available to all developers.

    Use database of course for settings that may be altered regularly.

  • #24 / Aug 22, 2009 5:58am

    Mikle

    30 posts

    Widget config should maybe be part of config/application.php

    I always have a dedicated application config file, this keeps all site settings available to all developers.

    Use database of course for settings that may be altered regularly.

    And if I need to set count of posts which widget renders, I should store this number in config/application.php?

  • #25 / Aug 22, 2009 6:03am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Or database, This is your choice. 😉

  • #26 / Aug 22, 2009 6:09am

    Mikle

    30 posts

    @Mikle, You make me work so hard these days. 😊

    Any method not declared specifically is assumed to be public method.

    Widget class does not need private method.

    I’m sorry for so much questions, but I’m newbee and learning codeigniter, and I love you extentions for CI, that save so much time in web application development. You can add paypal donation button to your site and I and many other people will send donations for your great job!

  • #27 / Aug 22, 2009 6:15am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    It’s ok, your questions are good ones. And thank you for the nice feedback.

    If people want to donate they can always send a private message via my profile.

  • #28 / Sep 06, 2009 5:47am

    Milos Dakic's avatar

    Milos Dakic

    114 posts

    is it possible to make this widget plugin behave with ME5? I want to create a separate widget for each module and all this will come together in a dashboard type view.

  • #29 / Sep 06, 2009 6:52am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Hi Milos, I’m sure we could work something out. I’ll do some testing.

  • #30 / Sep 06, 2009 6:57am

    Milos Dakic's avatar

    Milos Dakic

    114 posts

    I’m currently trying to work something out. I can get it to load the widget from a module, but when it comes to the view, well I’m having a hard time :(

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases