EllisLab text mark
Advanced Search
1 of 6
1
   
Widget plugin (intelligent view partials)
Posted: 24 March 2009 12:29 AM   [ Ignore ]
Avatar
Joined: 2007-06-10
2919 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($args1));    
    
}
    
    
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); ?> 
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 24 March 2009 12:32 AM   [ Ignore ]   [ # 1 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 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->uid86500);
                
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
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 24 March 2009 12:47 AM   [ Ignore ]   [ # 2 ]   [ Rating: 0 ]
Avatar
Joined: 2006-07-27
2617 posts

Looks nice and elegant. Good stuff.

 Signature 

Check out the Template Library
Oh yeah, I tweet, too (regarding CodeIgniter on occassion).

 
Posted: 24 March 2009 01:06 AM   [ Ignore ]   [ # 3 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 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.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 24 March 2009 03:02 AM   [ Ignore ]   [ # 4 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

Isn’t this just another way of creating modules?

 
Posted: 24 March 2009 05:54 AM   [ Ignore ]   [ # 5 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 posts

Modules were supposed to be self contained mini applications.

These Widgets allow you to create intelligent view partials.

See the difference? wink

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 24 March 2009 05:56 AM   [ Ignore ]   [ # 6 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

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

 
Posted: 24 March 2009 07:36 AM   [ Ignore ]   [ # 7 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 posts

Thanks for the intelligent reply xwero.

 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 24 March 2009 07:57 AM   [ Ignore ]   [ # 8 ]   [ Rating: 0 ]
Avatar
Joined: 2008-01-03
728 posts

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

 Signature 

Blog - Twitter

DBlog

MeNeedz: Auth - Cloud - Password - Search - Shoutbox - Akismet -
Twitter - Visitor tracking

 
Posted: 24 March 2009 08:21 AM   [ Ignore ]   [ # 9 ]   [ Rating: 0 ]
Joined: 2006-07-14
4237 posts

Sometimes i have to express myself smile

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.

 
Posted: 07 April 2009 08:51 AM   [ Ignore ]   [ # 10 ]   [ Rating: 0 ]
Joined: 2009-04-07
8 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.

 
Posted: 07 April 2009 12:29 PM   [ Ignore ]   [ # 11 ]   [ Rating: 0 ]
Avatar
Joined: 2007-06-10
2919 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(); 
 Signature 

URI Language Identifier | Modular Extensions - HMVC | View Object | Widget plugin | Access Control library

 
Posted: 07 April 2009 01:13 PM   [ Ignore ]   [ # 12 ]   [ Rating: 0 ]
Joined: 2009-04-07
8 posts

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

 
Posted: 28 April 2009 12:28 PM   [ Ignore ]   [ # 13 ]   [ Rating: 0 ]
Joined: 2009-04-28
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!

 
Posted: 14 May 2009 01:36 AM   [ Ignore ]   [ # 14 ]   [ Rating: 0 ]
Joined: 2006-06-30
3 posts

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

 
Posted: 27 May 2009 05:45 PM   [ Ignore ]   [ # 15 ]   [ Rating: 0 ]
Joined: 2009-01-23
2 posts

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

 
1 of 6
1