[eluser]wiredesignz[/eluser]
This is the View Library (application/libraries/View.php) it is autoloaded at startup.
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* View Library
*
* Useage:
* 1: Renders a layout with partials (blocks) inside.
* 2: Renders partials only (header,content,footer. etc).
* 3. Allows a plugin to render a partial.
*
* Allows a view source directory to be specified.
*
* @author Wiredesignz (c) 2007-12-25
*/
class View
{
var $layout, $data, $partials = array();
function View($file = NULL) //specify a layout file to use
{
$this->layout = $file;
static $data = array(
'directory' => '',
'module' => '',
);
$this->data = &$data; // make $this->data static
}
function set($key, $value = NULL) // set data for this view
{
if (is_array($key))
{
$this->data = array_merge($this->data, $key);
}
elseif ($value != NULL)
{
$this->data[$key] = $value;
}
}
function load($view, $file = NULL) //load partials as nested objects
{
if (is_array($view))
{
foreach ($view as $k => $v)
{
$this->reload($k, $v);
}
}
else $this->reload($view, $file);
}
function reload($view, $file)
{
$this->partials[$view] = (is_object($file)) ? $file : new View($file);
}
function get($key = NULL) //returns data value(s) from this view
{
return ($key) ? (isset($this->data[$key])) ? $this->data[$key] : NULL : $this->data;
}
function render($render = FALSE) // create the page (HTML)
{
if ($this->layout)
{
$CI = &get;_instance();
$CI->load->vars($this->partials);
return $CI->load->view($this->data['directory'].$this->layout, $this->data, $render, $this->data['module']);
}
else
{
foreach($this->partials as $k => $v)
{
$v->render();
}
}
}
}
View library updated 2008-02-06