This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoFill in empty topics, these should be proper code examples and not repeat general/views

View Class

For a more in depth overview of views see General/Views.

Creating an instance

Creating an instance of the view class.

$view = new View('welcome');

Setting data

There are several ways to pass data into your views.

Using the View as an object:

// Load the view as an object
$view = new View('yourview');
 
// Adding variables to the object that will be displayed in the view
$view->title   = "Welcome to Kohana !";
$view->heading = "My Heading";
$view->content = "My content here.";
 
//------- or --------
 
$view->set('title', "Welcome to Kohana !");
$view->set('heading', "My Heading");
$view->set('content', "My content here.");

You can also supply an array and the keys and values will be treated as variables:

// Load the view as an object
$view = new View('yourview', array('title' => "Welcome to Kohana !"));

Methods

set()

set() can be used to set a variable in a view. You can also supply an array and the keys and values will be treated as variables. $this→view→your_variable can be used to accomplish the same.

$view = new View('welcome');
 
$view->set('title', 'Elvis lives');

bind()

bind() is like set only the variable is assigned by reference.

$view = new View('welcome');
 
$var='Some value';
 
$view->bind('title', $var);
 
$var='Another value';
 
$view->render(true); //The 'title' variable will contain 'Another value'

set_global()

set_global() is like set only that the variables set are available throughout all views. This means you can use it with views in views for example.

// loading views
$view = new View('page');
$view->header = new View('header');
 
// setting variables in all views
$view->set_global('title', 'Title of page'); // set variable $title for example in view header.php
 
$view->render(TRUE);

render()

render() renders the output of the View.

// render and store, default, no browser output
$this->template->content = $this->session->get_once('message').$content->render();
 
// render output of view to browser
$this->template->render(TRUE);

factory()

This method is static. Parameters are the same as creating a new instance.

It creates a View instance and immediately returns it so method chaining is possible.

public function _add_breadcrumb()
{
    $crumbs = View::factory('admin/breadcrumb')
        ->set('crumbs', html::breadcrumb())
        ->render();
 
    $this->template->content = $crumbs.$this->template->content;
}

set_filename()

set_filename() sets the name of the file used for the view.

$view=new View;
 
if(request::is_ajax()) //request helper also exists in 2.2
{
   $view->set_filename('ajax_view');
}
else
{
   $view->set_filename('html_view');
}
 
$view->render(TRUE);

« Unicode : Previous