-
Notifications
You must be signed in to change notification settings - Fork 7.6k
View Object
Category:Library::Template Engine | Category:Library::Community
[h3]Introduction[/h3] This library provides you with a view object in which to build your page views from partials (blocks). The view object is available to controllers and models in your application to store data and add partials as needed.
Unlike other libraries and the native CI view loader, this view object does not create HTML until you use the render() method.
[h3]Source[/h3] [code] <?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 object to render a partial.
-
@copyright Copyright (c) Wiredesignz 2010-10-31
-
@version 0.0.1
-
Permission is hereby granted, free of charge, to any person obtaining a copy
-
of this software and associated documentation files (the "Software"), to deal
-
in the Software without restriction, including without limitation the rights
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-
copies of the Software, and to permit persons to whom the Software is
-
furnished to do so, subject to the following conditions:
-
The above copyright notice and this permission notice shall be included in
-
all copies or substantial portions of the Software.
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-
THE SOFTWARE. **/ class View { public $layout; private static $partials = array(), $vars = array();
public function __construct($file = NULL, $data = NULL) /* assign a template & data */ { $this->layout = $file; if (is_array($data)) $this->set($data); }
public function load($view, $template = NULL, $data = NULL) /* add a partial & data */ { if ( ! isset(self::$partials[$view])) { self::$partials[$view] = (is_object($template)) ? $template : new self($template); if (is_array($data)) self::$partials[$view]->set($data); }
return self::$partials[$view];
}
public function __set($partial, $template = NULL) { return $this->load($partial, $template); }
public function set($var, $value = NULL) /* store data for this view */ { ($var) ? (is_array($var)) ? self::$vars = $var : self::$vars[$var] = $value : NULL; }
public function __get($partial) { return (isset(self::$partials[$partial])) ? self::$partials[$partial] : NULL; }
public function get($var = NULL) /* returns data value(s) */ { return ($var) ? (isset(self::$vars[$var])) ? self::$vars[$var] : NULL : self::$vars; }
public function __toString() {
return $this->render(TRUE); }public function render($render = FALSE) /* render the view */ { $ci = CI_Base::get_instance();
$ci->load->vars(self::$partials); if ($this->layout) { return $ci->load->view($this->layout, self::$vars, $render); } else { ob_start(); foreach(self::$partials as $partial) { $partial->render(self::$vars); } if ($render) return ob_get_clean(); echo ob_get_clean(); }
} } [/code]
[h3]Usage[/h3]
[h4]Example usage in controller[/h4] [code]$this->load->library('view'); // or autoload
$this->view->layout = 'admin/layout'; //or leave empty to render partials only
$this->view->set(array( // set the view data
'privileges' => $privileges,
'catcode' => -1,
'page' => $page,
));
$this->view->load('header','common/header'); // load the partial
$this->view->menu = 'admin/menu'; // or load the partial like this
$this->view->content = 'admin/'.$page;
$this->view->load('footer', 'common/footer');
$this->view->render(); // create the view or $example = $this->view->render(TRUE) // assign to a variable[/code]
[h4]Example usage in layout using partials[/h4] [code]<?php $header->render(); ?>
<body> <?php $menu->render(); ?>
<?php $footer->render(); ?>[/code]