views:

62

answers:

3

When we load a view, we can pass some dynamic data to it.

What I would like to achieve is when the view receives no data, it uses some default data that is loaded from a model (database).

The problem is I don't really want to put these statements (that loads the default data) in the view.

What is the simplest solution available, without using any extension like modular extension/ separation?

Many thanks to you all.

+3  A: 

Use Base Controllers to get global data into your views.

Phil Sturgeon
+1  A: 

Something I came out with, hope this helps.

class Test extends Controller
{
    private $data = array();

    public function __construct()
    {
        parent::__constuct();

        // Load the default data
        $this->load->model('test_model');

        $this->data = $this->test_model->get_default_data();
    }

    public function test()
    {
        $this->load->model('test_model');
        $data = $this->test_model->get_another_data();

        if ( ! empty($data)) {
            $this->data = $data;
        }

        $this->load->view('test_view', $this->data);
    }
}
leonardys
+1  A: 

Your correct that you don't really want to call the Model from the View (that's what the Controller is for).

Check the data while in the Controller; if the data your looking for isn't there, make the call to the Model for the "default data" (while your in the Controller). Then pass the data to the View.

Mitchell McKenna