Part of the EllisLab Network
   
1 of 8
1
Widget plugin (intelligent view partials)
Posted: 23 March 2009 11:29 PM   [ Ignore ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

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

Profile
 
 
Posted: 23 March 2009 11:32 PM   [ Ignore ]   [ # 1 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

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

Profile
 
 
Posted: 23 March 2009 11:47 PM   [ Ignore ]   [ # 2 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  2774
Joined  07-27-2006

Looks nice and elegant. Good stuff.

 Signature 

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

Profile
 
 
Posted: 24 March 2009 12:06 AM   [ Ignore ]   [ # 3 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

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

Profile
 
 
Posted: 24 March 2009 02:02 AM   [ Ignore ]   [ # 4 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

Isn’t this just another way of creating modules?

Profile
 
 
Posted: 24 March 2009 04:54 AM   [ Ignore ]   [ # 5 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

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

Profile
 
 
Posted: 24 March 2009 04:56 AM   [ Ignore ]   [ # 6 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

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

Profile
 
 
Posted: 24 March 2009 06:36 AM   [ Ignore ]   [ # 7 ]  
Sr. Research Associate
Avatar
RankRankRankRankRank
Total Posts:  3216
Joined  06-10-2007

Thanks for the intelligent reply xwero.

 Signature 

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

Profile
 
 
Posted: 24 March 2009 06:57 AM   [ Ignore ]   [ # 8 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  775
Joined  01-03-2008

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

Profile
 
 
Posted: 24 March 2009 07:21 AM   [ Ignore ]   [ # 9 ]  
Sr. Research Associate
RankRankRankRankRank
Total Posts:  4785
Joined  07-14-2006

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.

Profile
 
 
Posted: 07 April 2009 07:51 AM   [ Ignore ]   [ # 10 ]  
Summer Student
Total Posts:  8
Joined  04-07-2009

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.

Profile
 
 
   
1 of 8
1