0

I am trying to implement a widgets library using load->view. I know I can use include to call directly the file and avoid the vars cache issues but just wondering why it does not work.

Here is how I have structured my code:

My Controller:

class Page extends MY_Controller {

    public $data = array();

    public function __construct() {
        parent::__construct();
        ...
        $this->load->library('widgetmanager');
    }

    public function index($slug = '') {

        echo $this->widgetmanager->show(2);
        echo $this->widgetmanager->show(1);

    }
}

My Library

class WidgetManager
{

    private $CI;

    public function __construct()
    {
        $this->CI = & get_instance();
    }

    public function show($widget_id) {
        $data = array();
        $widget_id = (int)$widget_id;



        $this->CI->db->select('*');
        $this->CI->db->from('widget');
        $this->CI->db->where('id', $widget_id);

        $query = $this->CI->db->get();

        $item = $query->row_array();

        $data['widget_title'] = $item['title'];
        $data['widget_content'] = $item['content'];

        $widget =  $this->CI->load->view('widget/'.$item['source'], $data, TRUE);

        $data['widget_title'] = '';
        $data['widget_content'] = '';

        $this->CI->load->view('widget/'.$item['source'], $data);

        return $widget;
    }
}

widget 1: Calls widget/content
widget 2: Calls widget/banner

What is happening is, the vars set on the first widget call (they are same name as second widget call), get cached, meaning values from the first call are passed to same call. It is weird because are different views.

I have tried:

  • Using clear_vars(): $this->CI->load->clear_vars(), before and after doing load->view on the library.
  • Calling load->view with empty array, null, etc
  • Tried to add a prefix with the widget slug to the vars (that works, but I have to send in some way the prefix to the view, so it is not possible due cache issue)

Any ideas?

Eduardo
  • 1,781
  • 3
  • 26
  • 61

1 Answers1

0

Here is what should work.

(I took the liberty of simplifying your database call making it require much less processing.)

public function show($widget_id)
{
    $data = array();
    $widget_id = (int) $widget_id;

    $item = $this->CI->db
      ->get_where('widget', array('id' => $widget_id))
      ->row_array();

    $data['widget_title'] = $item['title'];
    $data['widget_content'] = $item['content'];

    $widget = $this->CI->load->view('widget/'.$item['source'], $data, TRUE);

    //clear the cached variables so the next call to 'show()' is clean
    $this->CI->load->clear_vars(); 

    return $widget;
}

On further consideration The call $this->CI->load->clear_vars(); is probably pointless because each time WidgetManager::show() is called the $data var is recreated with exactly the same keys. When the $data var is passed to load->view the new values of $data['widget_title'] and $data['widget_content'] will replace the values in the cached vars using those keys.

DFriend
  • 8,869
  • 1
  • 13
  • 26
  • just tried, unfortunately it does not work, it keeps happening the same. – Eduardo Sep 22 '17 at 21:31
  • I want to make sure I understand the problem. You show two different widgets (different titles and content). But what get displayed is the same widget content twice? – DFriend Sep 22 '17 at 21:39
  • thanks a lot for taking the time to help me. I have a folder called "widget" inside "views" folder. Inside of it, I have different files, in example: content, banner, steps. Content is my general widget, used for most cases. I am looking to load the content processed of those widgets to then load them to my page through action page/index in my controller. – Eduardo Sep 22 '17 at 21:48
  • You are welcome. Happy to help. I think I understand what your goal is. I am trying to understand exactly what gets displayed differs from what you expect. – DFriend Sep 22 '17 at 21:52
  • On the first call, the variables: $widget_title and $widget_content have the values expected, in the second call, those same vars, still have the values from the first call. – Eduardo Sep 22 '17 at 21:53
  • I tested the revised answer above. The only reason I can think of that you see the same title and content is if the database records are the same. – DFriend Sep 22 '17 at 22:23
  • No, they are different. I am running version 3.0.0 – Eduardo Sep 22 '17 at 22:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/155127/discussion-between-dfriend-and-eduardo). – DFriend Sep 22 '17 at 22:26