I’m using another way to nest views in views and I wanted to submit it to you, CI gurus, as I’m not sure if it’s a good practice.
Better than a long and complicated explanation, here is some sample code:
First, you have to set up a post_controller hook.
The post-controller hook is the layout manager:
define('DEFAULT_LAYOUT', 'mainLayout');
function layout()
{
global $CI, $OUT;
$layout = isset($CI->data->layout) ? $CI->data->layout : DEFAULT_LAYOUT;
$layout();
$OUT->set_output($CI->load->view($layout, $CI->data, TRUE));
}
function mainLayout()
{
global $CI;
/* Layout data */
// window title
if (isset($CI->data->windowTitle)) {
$CI->data->windowTitle = ' - ' . $CI->data->windowTitle;
} else {
$CI->data->windowTitle = '';
}
/* Content data */
// Page title
if (!isset($CI->data->breadcrumbs)) {
$CI->data->breadcrumbs = array();
}
if (isset($CI->data->pageTitle)) {
$CI->data->pageTitle = $CI->load->view('titleView', $CI->data, TRUE);
} else {
$CI->data->pageTitle = '';
}
// Content
if (isset($CI->data->content)) {
$CI->data->content = $CI->load->view($CI->data->content.'View', $CI->data, TRUE);
} else {
$CI->data->content = '';
}
}
The controller just fetches data:
class Home extends Controller
{
...
function contact()
{
$this->data->pageTitle = $this->data->windowTitle = 'contact';
$this->data->active = '/contact';
$this->data->content = 'contact';
}
...
}
Finally, views:
* the main one:
<html>
<head>
<title>mysite.com<?=$windowTitle?></title>
</head>
<body>
<div id="content">
<?=$content?>
</div>
</body>
</html>
* the content view which contains an another embedded view
<div id="left">
<?=$pageTitle?>
<p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas sed augue.</p>
</div>
* the title view
<h1><?php
foreach($breadcrumbs as $url => $label) {
if ($label != $pageTitle) {
echo "<a title=\"$label\" href=\"$url\">$label</a> > ";
}
}
echo $pageTitle
?></h1>;
Sorry for this long post but I wanted to be the more explicit I could.
The concept is to use the post-controller hook as a layout manager. This way, no need to repeat the same code for each controller managing views.
Comments and feedback would be appreciated.