[eluser]IanJ[/eluser]
After playing with CI for a couple of hours, I came up with this little solution I added to the loader.
Code:
/*------------------------------------------------------------
|
| viewLayout($template, $data = array(), $layoutData = array(), $layout = 'default')
|
| Renders output with a layout, rather than using
| two statements in the code, this will do the same
| in one. Layouts are stored in the views/layouts
| directory.
|
| Required:
| $template - the tempalte to render as normally would
| with $this->load->view('template');
|
| Optional:
| $data - the data to be rendered with the template
|
| $layoutData - data to be included in the layout, DO NOT
| USE content_for_layout in the $layoutData
| array
|
| $template - the template to render, defaults to default
|
+------------------------------------------------------------*/
function layout($template, $data = array(), $layoutData = array(), $layout = 'default'){
$layoutData['content_for_layout'] = $this->_ci_load(array('view' => $template, 'vars' => $this->_ci_object_to_array($data), 'return' => true));
$this->_ci_load(array('view' => "layouts/$layout", 'vars' => $this->_ci_object_to_array($layoutData), 'return' => false));
}
It treats the layout as just another view so you can pass a normal array of variables in and parse them in the layout just like a normal view. Just make sure to add
Code:
<?php echo $content_for_layout; ?>
to your layout files.
Ian