Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 21 revisions

This is a totally 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)

[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); /**

  • View Object PHP5

  • 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.3 Wiredesignz (c) 2008-08-16 **/ class View { public $layout;

    private $partials = array();

    private $vars = array();

    private static $ci;

    public function __construct($file = NULL, $data = NULL) //specify a layout file to use { (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) //create a partial { if ( ! isset($this->partials[$view]))

          $this->partials[$view] = (is_object($file)) ? $file : new View($file);
      
      $this->partials[$view]->set($data);
      
      return $this->partials[$view];
    

    }

    public function __set($variable, $value) { if (is_array($value))

          $this->set($value); 
          
          else $this->set($variable, $value);
    

    }

    public function set($var, $value = NULL) //store data for this view { if (is_array($var))

          $this->vars = array_merge($this->vars, $var); 
          
          else $this->vars[$var] = $value;
    

    }

    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) // create the page { if ($this->layout) { self::$ci->load->vars($this->partials); return self::$ci->load->view($this->layout, $this->vars, $render); } else { ob_start();

          foreach($this->partials as $partial) 
    
              $partial->render();
              
          if ($render) return ob_get_clean();
          
          echo ob_get_clean();
      }
    

    } } [/code]

[size=2]Usage:[/size]

Load the View library from application/libraries. [code]

$this->load->library('view');

[/code]

Add a master layout (template) file [code]

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

[/code]

Add data to the master template view [code]

$this->view->set($site->data);

[/code]

Add a partial file and (optional) $data [code]

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

[/code]

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

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

[/code]

Add data to any partial [code]

$header->set($data);

[/code]

Render your View [code]

$this->view->render();

// using __toString()

echo $this->view

[/code]

Inside your master template [code]

<?php echo $header; ?>

[/code]

Clone this wiki locally