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]
  • #1 / Mar 24, 2009 12:29am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Widget PHP5 only version is available on page 2 of this thread

    Add some intelligence to your view partials. Thanks to the Yii framework for this idea.

    <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
    /**
     * Widget Plugin 
     * 
     * Install this file as application/plugins/widget_pi.php
     * 
     * @version:     0.1
     * $copyright     Copyright (c) Wiredesignz 2009-03-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 Widget() {
            $this->_assign_libraries();
        }
        
        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 _assign_libraries() {
            $ci =& get_instance();
            foreach (get_object_vars($ci) as $key => $object) {
                $this->$key =& $ci->$key;
            }
        }
    }

    Autoload the plugin, $autoload[‘plugin’] = array(‘widget’);

    Create your widgets in a new application/widgets directory, and add a views subdirectory.

    Widgets have access to the CI core and can load libraries themselves.

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

    <?php widget::run('user_login', (bool) $this->user->is_guest); ?>
  • #2 / Mar 24, 2009 12:32am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    An example widget class, application/widgets/user_login.php

    class User_login extends Widget
    {
        function run($visible = FALSE) {
            
            if ($post = $this->input->post('loginform')) {
                if ($post['username'] == 'admin') {
                    $query = $this->db->query("SELECT uid FROM user WHERE username='admin'");
                    $result = $query->row();
                    
                    set_cookie('ci_user', $result->uid, 86500);
                    redirect();
                }
            }
            
            if ($visible) $this->render('user_login');
        }
    }

    and its view, application/widgets/views/user_login.php

    <div class="widget">
    <div class="header">Login</div>
    <div class="content">
    <form action="" method="post">
    <div class="row">
    <label for="loginform_username">Username</label><br>
    <input name="loginform[username]" id="loginform_username" type="text" value="" /></div>
    <div class="row">
    <label for="loginform_password">Password</label><br>
    <input name="loginform[password]" id="loginform_password" type="password" value="" /></div>
    <div class="row">
    <input type="hidden" value="0" name="loginform[rememberme]" /><input name="loginform[rememberme]" id="loginform_rememberme" value="1" type="checkbox" ><label for="loginform_rememberme">Remember me next time</label></div>
    <div class="row">
    <input type="submit" value="Login" /></div>
    </form>
    </div></div>
  • #3 / Mar 24, 2009 12:47am

    Colin Williams's avatar

    Colin Williams

    2601 posts

    Looks nice and elegant. Good stuff.

  • #4 / Mar 24, 2009 1:06am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Thanks.

    I’m porting the Yii blog demo to CodeIgniter just to see how different the development process is between the two frameworks and use this widget plugin.

  • #5 / Mar 24, 2009 3:02am

    xwero

    4145 posts

    Isn’t this just another way of creating modules?

  • #6 / Mar 24, 2009 5:54am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Modules were supposed to be self contained mini applications.

    These Widgets allow you to create intelligent view partials.

    See the difference? 😉

  • #7 / Mar 24, 2009 5:56am

    xwero

    4145 posts

    tomato, tomato. hey that doesn’t work in writing. film, movie then 😛

  • #8 / Mar 24, 2009 7:36am

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Thanks for the intelligent reply xwero.

  • #9 / Mar 24, 2009 7:57am

    davidbehler's avatar

    davidbehler

    708 posts

    Thanks alot!
    I was gonna do something similar for my new project, but I guess I’ll be using this one instead!

  • #10 / Mar 24, 2009 8:21am

    xwero

    4145 posts

    Sometimes i have to express myself 😊

    A change i would make is to create a widgets directory in the views directory if you really consider the widgets to be partials. Containing the views to the widgets directory gave me the module feel.

    And where are the models of the widgets going to be located? Your widget example suggests the database library is loaded.

  • #11 / Apr 07, 2009 8:51am

    RobertSall

    7 posts

    Hi!

    I’m using this code to create widgets, but I came to a problem. In a widget I’m trying to use the form_validation library. When I just use it the class dosen’t return any errors even tho I leave the fields empty. But when I’ve tryed to extend the Wdiget form to the Controller class it worked with the form_validation, but then the classes that should be autoloaded didn’t load so I couldn’t use a few objects (session in this example).

    Here’s some code:

    class Login extends Widget
    {
        
        function run()
        {
            $this->load('form_validation');
            $this->load->database();
            
            #$this->load('session');
            
            #$this->session->set_userdata('test');
    
            
            $this->form_validation->set_rules('username','Användarnamn','required|callback__check_user_pw');
            $this->form_validation->set_rules('password','Lösenord','required');
            
            $this->form_validation->set_message('required','Du måste fylla i %s');
            
            if ($this->form_validation->run() == FALSE)
            {
                echo validation_errors();
                echo "incorrect";
            }
            else
            {
                echo "correkt";
            }
            }
    }

    Note this isn’t the real code, this just shows that the validation_errors() function, the word “incorrect” shows on the screen, but no validation errors.

  • #12 / Apr 07, 2009 12:29pm

    wiredesignz's avatar

    wiredesignz

    2882 posts

    Loading the Form_validation library in the widget makes it invisible to CI, hence the validation_errors() helper does not work correctly.

    Try using the Form_validation library methods which is what the helper does anyway.

    echo $this->form_validation->error_string();
  • #13 / Apr 07, 2009 1:13pm

    RobertSall

    7 posts

    I see, I worked it out with the function you said. Thank you 😊 .

  • #14 / Apr 28, 2009 12:28pm

    NoahTall

    1 posts

    My first post, and I am not new..just busy, so I just wanted to say, thank you, thank you very much, I love widgets, and this is just perfect for me…seems to work just fine, a nice, tidy little spot for widgets, that works just fine..

    I am a big fan of CI, I am running the latest backendpro and I modified the whole thing to use the ajaxify library thing, http://maxblog.me/ajaxify , and it all is working so nicely..adding widgets is a perfect end to a long day..

    Keep up the good work!

  • #15 / May 14, 2009 1:36am

    victorlenus

    3 posts

    A neat solution for a common issue, Thanks.
    BTW, how can I load a model from the widget. Is it possible?

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

ExpressionEngine News!

#eecms, #events, #releases