I know this thread is almost 2 years old… but it was the first that came up for my Google search and no one has given a definitive answer to:
1. Including children-views that have access to the same variables as the parent-view
2. Not having to call each view in each controller method
3. Setting master content for the sub-views
4. Setting method specific content for the sub-views
I was surprised to find out that not even the Template library suggested here could handle these four tasks, but this basic implementation can.
So here is my solution. It’s quite simple really:
In your controller, do the following
- ‘global’ is a standard string variable, but could be anything
- ‘header’ is a string containing the location of the header sub view
- ‘footer’ is a string containing the location of the footer sub view
public function __construct() {
parent::__construct();
// Set master content and sub-views
$this->load->vars( array(
'global' => 'Available to all views',
'header' => 'subviews/header',
'footer' => 'subviews/footer'
));
}
public function index() {
$data['title'] = 'Index Method';
$this->load->view('template', $data);
}
Then, in your template.php view, wherever you want your sub-views to appear:
<?php $this->load->view($header); ?>
<!-- Other Page Content Here -->
<?php $this->load->view($footer); ?>
Done like this, $global and $title can be used in all three views (template, subview/header, and subview/footer).
Is there a better solution?