-
Notifications
You must be signed in to change notification settings - Fork 7.6k
View Object
Category:Template Engines Category:Views Category::Libraries
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.
[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 to render a partial.
-
The source directory can be specified.
-
Works with Matchbox modules.
-
Version 2.1.0 Wiredesignz (c) 2008-02-18 **/ 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; //data is static
}
function load($views, $data = NULL) //store partials as nested objects { if (is_array($views)) { foreach ($views as $k => $v) { $this->set($k, $v); } } else $this->set(NULL, $views);
if (is_array($data)) { $this->data($data); }
}
function set($view, $file) //create or overwrite a partial { $this->partials[$view] = (is_object($file)) ? $file : new View($file); }
function data($data, $value = NULL) //store data for this view { if (is_array($data)) { $this->data = array_merge($this->data, $data);
} elseif ($value != NULL) { $this->data[$data] = $value; } }function fetch($key = NULL) //returns data value(s) { return ($key) ? (isset($this->data[$key])) ? $this->data[$key] : NULL : $this->data; }
function render($render = FALSE) // create the page { 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(); } } } } [/code]
Example using the View Object in a Controller: [code]
$this->load->library('view'); // or autoload
$this->view->layout = 'admin/layout'; //or leave empty to render partials only
$this->view->data(array( // set the view data
'privileges' => $privileges,
'catcode' => -1,
'page' => $page,
));
$this->view->load(array( // load the page partials
'header' => 'header',
'menu' => 'menu',
'content' => 'admin/'.$page,
'footer' => 'footer',
));
$this->view->render(); // create the view or $example = $this->view->render(TRUE) // assign to a variable
[/code]
Example of using partials inside the layout file: [code] <head> <? $header->render(); ?> </head> <body> <? $menu->render(); ?>