Code Igniter - Best Practice for views to not violate DRY Code Igniter - Best Practice for views to not violate DRY codeigniter codeigniter

Code Igniter - Best Practice for views to not violate DRY


I like to create a parent controller with a method like renderPage('content_view', $data). That method can include the header, menu, footer, ... That way, all the view loading stuff is kept in the controller and I don't have to bother with header, menu or footer on every action or view. It's also flexible as your child controllers can redefine the renderPage() method to fit their purposes.

If you need to load multiple content views, you could create a renderPage() method that takes in an array of string instead of a string.


Yes - have a template view. In your controller:

$data['header'] = xxx;$data['content'] = xxx;$this->load->view('my_template', $data);

Then in your my_template.php view file:

$this->load->view('head', $header);$this->load->view('volunteers/add_profile.php',$content);$this->load->view('foot'); 


Either what @TheShiftExchange suggested, or, if your application allows it, you can call header and footer views from each content view (which is the only view called from the controller then).