Rick Jolly shows me a good way to load my partials as libraries or plugins, using a helper function to do that. In addition, he did convince me about the advantages of load the partials directly from the view, instead of the controller. A view centric is decoupled from the controler, which is much more flexible and easy.
BizComputing did create another loader method to load my partials not from a library, but from a new folder, “modules”, that serve to better organize our files, with the advantage of use classes and not only functions.
WireDesignz told that another loader method is unnecessary, since CI already have methods to achieve this using libraries, plugins or helpers.
I was thinking about these opinions and about that in CI only one controller can run at a time. Thinking further, the user only interact with one controller at a time, because the web page only allows one form to be submitted at a time (unless you use AJAX in some special way). However, most of the time, we don’t need multiple interaction with user and the server. So, why we need a nested MVC anyway?
Our main concern here is to simplify our partials rendering. In complex web page layouts, normally we have one active partial, where the user is interacting (generally the bigger area in the middle of the page). The other partials are mostly passive, they only display data.
I was thinking: why do we need classes for our partials? I don’t think we even need functions for them. Think about it. You only need a script file that loads models and views, maybe some helpers and libraries. Well, you can create functions and classes in a script if you want, but the point is: where is really necessary to extend them from the main controller?
Thinking about the WireDesignz opinion, I was looking at the code of the Loader library, and I noticed that the load->helper() and load->library() was designed to load a file only once. This is useful to load files with functions that shouldn’t be loaded twice. I think load->helper don’t server the purpose of loading a partial, because in a layout we may need to load (and execute) the same partial more than once.
The load->plugin() and load->script() will load a file more than once. If you plan to use classes or functions, it will not work as expected. But load->script() is deprecated. It is almos the same of load->plugin(), except that load->plugin() will check in the application folder, and them the plugins folder under system folder.
The load->file() is a loader for a generic PHP file. It works almost the same way of load->view(). It executes the script, store the result buffer and return it, but do not extract vars as load->view() does. May this function is a good option to load our partials, as it can load any script we need. The only issue is passing to it the instance of the CI object. May our helper function could do this for us, if we extend the core and made our own $this->load->partial().
Examples:
// example controller:
Some_controller extends Controller {
function Some_controller() {
parent::Controller();
$this->load->model('model_file','model_name');
$data['page_title']='Title of this page';
$data['username']=$this->session->userdata('username');
$this->load->vars($data);
}
function index() { // for example, a list of products
// do something
$data['page_title']='Title of this page - index';
$data['content']=$load->view('some_view1',$any_data);
$any_data['center_page']=$this->model_name->function();
$this->load->view('main_view',$data);
}
function action1($id) { // for example, a form to add a product
// do something
$data['page_title']='Title of this page - action1';
$any_data['center_page']=$this->model_name->function2();
$data['content']=$load->view('some_view2',$any_data);
$this->load->view('main_view',$data);
}
function action2($id) { // for example, a form to edit a product
// do something
$any_data['page_title']='Title of this page - action2';
$any_data['center_page']=$this->model_name->function3();
$data['content']=$load->view('some_view3',$any_data);
$this->load->view('main_view',$data);
}
}
// some_view:
<div id="leftside"><?=load_partial('left_side')?></div>
<div id="centerpage"><?=$center_page?></div>
<div id="rightside"><?=load_partial('right_side')?></div>
// load_partial() helper:
function load_partial($filename) {
$ci=&get;_instance();
// load our partials from its own folder:
$partial=$ci->load->file(APPPATH.'partials/'.$filename,TRUE);
return $partial;
}
// left_side partial:
$ci=&get;_instance();
$ci->load->model('some_model','model_name');
$any_data['partial_content']=$ci->model_name->function();
$ci->load->view('partial_view',$any_data);
// right_side partial:
$ci=&get;_instance();
$ci->load->model('some_model2','model_name2');
$any_data['partial_content2']=$ci->model_name2->function();
$ci->load->view('partial_view2',$any_data);
I think it works. If anybody want more details, I am glad to explain.