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

Category:Library::Template Engines | 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 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]

[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->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]

[h4]Example usage in layout using partials[/h4] [code]<? $header->render(); ?>

<body> <? $menu->render(); ?>

<? $content->render(); ?>
</body>

<? $footer->render(); ?>[/code]

Clone this wiki locally