Welcome Guest, Not a member yet? Register   Sign In
Widget plugin (intelligent view partials)
#71

[eluser]vee[/eluser]
@umefarooq
thanks but not that way..
i want it just return value from $this->render()
NOT echo without echo. (please try to call widget from topic example without echo and you will know.)


++++++
from this page http://ellislab.com/forums/viewthread/109584/P20/
Quote:Updated Widget plugin for use with Modular Extensions PHP5

my widget
Code:
class test extends widget {
    
    
    public $title = "Test";
    public $description = "For test";


    function run() {
        $this->render("test");
    }
    
    
}
test.php



Code:
<div style="border: 1px dotted #ccc; margin-bottom: 10px;">
    test widget
</div>
views/test.php


call widget by
Code:
&lt;?php widget::run(‘test’); ?&gt;
it will echo, print widget. but i want to keep it in variable, $string

... i want this
Code:
&lt;?php
$widget = widget::run('test');
// do something with widget
$widget = str_replace(...);
?&gt;
i cannot make it.
#72

[eluser]Unknown[/eluser]
Thank you wiredesignz for code and support.
I have slightly modified the code to each widget has been placed in its directory:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Widget Helper
*
* http://ellislab.com/forums/viewthread/109584/
*
* Install this file as application/helpers/widget_helper.php
*
* @version:     0.2m
* $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.
*
* @modified by jen <[email protected]> 2012-03-13
* @to place each widget in its directory
*/
class Widget
{
private static $name;

function run($name) {

  self::$name = $name;
  
  $args = func_get_args();
  
  require_once APPPATH.'widgets/'.$name.'/index'.EXT;
  $name = ucfirst($name);
  
  $widget = new $name();
  return call_user_func_array(array($widget, 'run'), array_slice($args, 1));    
}

function render($data = array()) {

  extract($data);
  include APPPATH.'widgets/'.self::$name.'/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;
}
}

/* End of file widget_helper.php */
/* Location: ./application/helpers/widget_helper.php */

[quote author="wiredesignz" date="1289633500"]Change the file name to widget_helper.php and relocate the file to application/helpers.[/quote]

1. Create a directory with the name of the widget in the /application/widgets .

2. Place there
index.php - widget class file (Class has the same name as the directory)
view.php

Notice that render() method now get only one argument - $data.

It looks like

/application/widgets/

my_widget/

index.php
view.php
#73

[eluser]Karman de Lange[/eluser]
Thanks very much for this code, works perfect! :cheese:

I extended it a bit so I can keep with the MVC theme and keep my data function in own widget model.

Simply add following (probably better way, but works for me)
Code:
function model($model=''){
        if(!$model){  // if no model defined, just load the one in the widget folder
        $this->load->model('../widgets/'.self::$name.'/'.self::$name.'_model');  //calls widget_name_model ,
        }else { //load supplied model
         $this->load->model('../widgets/'.self::$name.'/'.$model);  //calls widget_name_model,
        }

          
    }

to use it:

Code:
$this->model(); //load default model.
//or $this->model('model_name')

Just reference model as normal in your code
Code:
$this->widget_name_model->test(); //calls test() function.
#74

[eluser]boytun[/eluser]
Hello Wiredesignz.

Really great job, congratulation Smile .
So my question is, how can I use your widget system if I want to apply the HMVC architecture ? .

Thanks in advance
#75

[eluser]wiredesignz[/eluser]
Widgets have no specific relation to the HMVC architecture other than supporting the (MVC) View component as partials.
#76

[eluser]boytun[/eluser]
Thanks for your fast reply.
I try to make a test ,using widget_helper.php and your example in the first page'User_login'.
I fellow the steps, like in the jen's post, but I get this error:

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'); } }application/widgets/User_login/index.php
Fatal error:
Class 'User_login' not found in C:\xampp\htdocs\application\helpers\widget_helper.php on line 49

but When copy and past the class of the' User_login' in the 'widget_helper', the view is rendred but I get 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'); } }

A PHP Error was encountered

Severity: Warning

Message: extract() expects parameter 1 to be array, string given

Filename: helpers/widget_helper.php

Line Number: 69

I'm working on the Project of the End of Studies and I really need your help
I'm waiting for your answer as soon as possible.

Thanks
#77

[eluser]galdikas[/eluser]
Hi,

Sorry about resurrecting this, but I thought it would be more appropriate than starting new topic.

So basically if i use the:

Code:
$autoload[‘plugin’] = array(‘widget’);

I get the fatal error: class widget not found (in my view file).

I managed to get it to work by adding this line to the autoload.php file:

Code:
include(APPPATH."plugins/widget_pi.php");

But even though I am not CI hacker, I have a feeling I should not be doing this (even though it appears to be working). I am using the 3.0-dev version (not sure what the dev means here.. i just donloaded it from CI homepage, this is the value of "CI_VERSION"...

So anyway can I leave it the way it is? Or should I fix it? And if latter is true, any ideas how could I make it work in standard way?

#78

[eluser]wiredesignz[/eluser]
Plugins have been deprecated since CI 2.0.0

You could convert the widget plugin into a helper or library as needed.

Or you could extend the CI_Loader library with a plugin loader method.

Such as follows which is copied from CI1.7.2 but not tested in CI2.1.x or CI3.0

EDIT: Also note that the EXT constant has been deprecated now so you'll need to replace all occurrences of EXT in the widget plugin with '.php'

Code:
class MY_Loader extends CI_Loader {

public $_ci_plugins = array();

/**
  * Load Plugin
  *
  * This function loads the specified plugin.
  *
  * @access public
  * @param array
  * @return void
  */
function plugin($plugins = array())
{
  if ( ! is_array($plugins))
  {
   $plugins = array($plugins);
  }

  foreach ($plugins as $plugin)
  {
   $plugin = strtolower(str_replace('.php', '', str_replace('_pi', '', $plugin)).'_pi');  

   if (isset($this->_ci_plugins[$plugin]))
   {
    continue;
   }

   if (file_exists(APPPATH.'plugins/'.$plugin.'.php'))
   {
    include_once(APPPATH.'plugins/'.$plugin.'.php');
   }
   else
   {
    if (file_exists(BASEPATH.'plugins/'.$plugin.'.php'))
    {
     include_once(BASEPATH.'plugins/'.$plugin.'.php');
    }
    else
    {
     show_error('Unable to load the requested file: plugins/'.$plugin.'.php');
    }
   }
  
   $this->_ci_plugins[$plugin] = TRUE;
   log_message('debug', 'Plugin loaded: '.$plugin);
  }  
}
}
#79

[eluser]galdikas[/eluser]
Thanks for your help! I knew that plugins was probably not supported anymore when I needed to create my own directory for it lol

I just ended up making it into a helper! Thanks for your help! Smile
#80

[eluser]Unknown[/eluser]
Hi,

a quick question:
How can i use jquery or ajax within a view of the widget?

I need to update some data which comes from db in a widget view periodicaly, so i have to send a flag in setInterval to the widget controller.

e.g. a friendslist-widget

how does that work?
how do i have tu set the ajax request url?

thanks for ur work..




Theme © iAndrew 2016 - Forum software by © MyBB