View Object PHP5

Derek Jones edited this page Jul 5, 2012 · 21 revisions
Clone this wiki locally

This is a PHP5 object oriented View library.

You can add a master template from which you can load View Objects as partials or you can use the View Object to render partials only. (ie: header, menu, content, sidebar, footer)

<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
* View Object
* Renders a layout with partials (blocks) inside.
* Renders partials only (header,content,footer. etc).
* Allows a plugin or module to render a partial.
* Version 3.0.7 Wiredesignz (c) 2008-10-01
class View
    public $layout;
    private $partials = array();
    private $vars = array();
    private static $ci;
    public function __construct($file = NULL, $data = NULL) /* you can assign a template & data */
        (isset(self::$ci)) OR self::$ci = get_instance();
        $this->layout = $file;
        (is_array($data)) AND $this->vars = $data;
    public function load($view, $file = NULL, $data = NULL) /* add a partial & data */
        if ( ! isset($this->partials[$view]))
            $this->partials[$view] = (is_object($file)) ? $file : new View($file);
        (is_array($data)) AND $this->partials[$view]->set($data);
        return $this->partials[$view];
    public function __set($variable, $value)
        (is_array($value)) ? $this->set($value) : $this->set($variable, $value);
    public function set($var, $value = NULL) /* store data for this view */
        ($var) ? (is_array($var)) ? $this->vars = $var : $this->vars[$var] = $value : NULL;

    public function __get($variable)
        return $this->fetch($variable);
    public function fetch($key = NULL) /* returns data value(s) */
        return ($key) ? (isset($this->vars[$key])) ? $this->vars[$key] : NULL : $this->vars;
    public function __toString()
        return $this->render(TRUE);
    public function render($render = FALSE) /* render the view */
        if ($this->layout)
            return self::$ci->load->view($this->layout, $this->partials, $render);
            foreach($this->partials as $partial) 

            if ($render) return ob_get_clean();
            echo ob_get_clean();


Load the View library from application/libraries.


Add a master layout (template) file

$this->view->layout = 'master_layout_file';    // or leave this empty to render partials only

Add data to the master template view



Add a partial file and (optional) $data

$header = $this->view->load('header', 'header_file', $data);

Partials are View objects too, so you can add partials to partials

$header->load('sub_header', 'sub_header_file', $data);

Add data to any partial



Render your View


// using __toString()

echo $this->view

Rendering your partials inside your master template

<?php $header->render(); ?>

// using __toString()

<?php echo $header; ?>


The View Object can use other objects (including modules) to render a partial. Simply add a render() method to your object and load it like any partial.

$this->view->load('login', $this->object);

When the View Object is called to render it will in turn call your Object->render() method.

$login->render(); // = $this->object->render();

//using __toString()

<?php echo $login; ?>

[size=5]Add Regions to Regions:[/size]

Content parts can be added to as you go, simply create a partial, and use the returned object to add as many sub-partials as are needed.

$content = $this->view->load('content');

foreach ($sub_contents as $part)
    $content->load($part->id, $part->partial, $part->data);

[size=5]Create Independent Regions:[/size]

View Objects can also be created independently, they can have their own partials and then they can be added to the master view.

$content = new View();


$this->view->load('content', $content);