Update 2 available here: http://codeigniter.com/forums/viewthread/62521/
Hello,
Let’s face it CodeIgniter is good, but sucks on partial views. So I created a layout based view on top of the existing view method that CI offers.
How it works:
I view can be a partial view (a part of the page) or a layout view which defines where the partial views are on the page. For ex:
layout view
<?=$header;?>
<?=$content;?>
<?=$footer;?>
header view
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
...
Now the tricky part comes in. In theory a partial view could be also a layout view. This is no problem for the library. You can set a new layout inside a master layout and add partials to that layout or create a new layout inside of it and so on…
As you can see I suck on writing tutorials. So I’m just going to paste here the library and attach an example files.
<?php
class Layout
{
var $obj;
var $_layout;
var $_parts = array();
var $_datas = array();
/**
* @param array If a config file exists CI autoloads it then we have to parse it.
*/
function __construct($configData = null)
{
$this->obj =& get_instance();
if ($configData)
{
$this->readConfigData($configData); //reading the config data
}
}
function readConfigData($data)
{
$parts = $data['parts'];
$this->_layout = $data['layout'];
foreach ($parts as $key => $value)
{
if ($value == ':layout:')
{
$this->_parts[$key] = new Layout($data[$key]);
$this->$key =& $this->_parts[$key];
}
else
{
$this->_parts[$key] = $value;
}
}
}
/**
* Passing a data array to a specific part view
*
* @param string The name of the partial view (not the file name)
* @param array The data to be passed to this partial view
*/
function addData($part,$data = array())
{
if ( $this->hasPart($part) )
{
if ( $this->hasData($part) )
{
$this->_datas[$part] = array_merge($this->_datas[$part],$data);
}
else
{
$this->_datas[$part] = $data;
}
}
}
/**
* Adds a partial view to the layout
*
* @param string The name of the partial view (this will be passed as a data array to the master layout)
* @param string The filename of the partial view (with or without a subfolder)
* @param array Optional data array to be passed to the partial view
*/
function part($part,$view,$data = array())
{
$this->_parts[$part] = $view;
$this->_datas[$part] = $data;
}
/**
* Creates a new layout
*
* @param string The name of the new layout
*/
function newlayout($part)
{
$this->_parts[$part] = new Layoutext();
$this->$part =& $this->_parts[$part];
}
/**
* Check is a partial view exists
*
* @param string The name of the partial view
*/
function hasPart($part)
{
return isset($this->_parts[$part]);
}
/**
* Check if a partial view has data
*
* @param string The name of the partial view
*/
function hasData($part)
{
return isset($this->_datas[$part]);
}
/**
* Sets the layout
*
* @param string The filename of the layout
*/
function layout($srcLayout)
{
$this->_layout = $srcLayout;
}
/**
* Gets the layout filename
*/
function getLayout()
{
return $this->_layout;
}
/**
* Outputs the whole layout
*
* @param bool Whether to output it to the bowser or return it for reuse.
*/
function view($return = false)
{
$tmp_parts = array();
$tmp_data = array();
foreach ($this->_parts as $part => $view)
{
if (isset($this->_datas[$part]))
{
$tmp_data = $this->_datas[$part];
}
if (is_object($view) )
{
$tmp_parts[$part] = $view->view(true);
}
else
{
$tmp_parts[$part] = $this->obj->load->view($view,$tmp_data,true);
}
}
$output = $this->obj->load->view($this->_layout,$tmp_parts,$return);
if ($return) {
return $output;
}
}
}
?>
The attached example contains test views, layouts and a test controller and also an autoloading config file. Just copy these files to you CodeIgniter project and load the test controller like this: http://ci_project/test/config_yes or http://ci_project/test/config_no
Actually this is the link to the example file: http://www.primalskill.com/
- ci_project is your CodeIgniter project.
- config_yes loads the layout library with the autoload config
- config_no loads the layout library and builds up the test_layout you have to delete the autoloading config file first!
I hope this library will be beneficial to all CodeIgniter users. If you have any question you can send me a PM or reply here. I’m happy to answer all of your questions.
