This is documentation for Kohana v2.3.x.

Table of Contents
StatusDraft
TodoFill in empty topics

Views

See Core:View class for more in depth information on using views in your code.

Overview

Views are files that contain the display information for your application. This is most commonly HTML, CSS and JavaScript but can be anything you require such as XML or Json for AJAX output. The purpose of views is to keep this information separate from your application logic for easy reusability and cleaner code.

While this is true, views themselves can contain code used for displaying the data you pass into them. For example, looping through an array of product information and display each one on a new table row. Views are still PHP files so you can use any code you normally would. Views are executed in the Controller namespace so you have access to all resources you have loaded into $this→

When this view is rendered it is executed just as any PHP script would and the output from it is returned (or sent to the browser if you so wish).

Creating views

Views need to be placed in the views directory. The filename minus the extension becomes the view's name. Views can be arranged in sub-directories if needed but the path must be specified when loading them.

Examples

// Filename home.php
$view = new View('home');
 
// Filename list.php in sub-directory 'products'
$view = new View('products/list');

Loading views

There are 3 ways to load a view. It is important to note that this simply creates an instance of the View class, at this point the view file has not been read and no output is created. This only happens when it is rendered.

New object

Create a new instance of the class.

$view = new View('welcome');

Factory

Use the factory() static method. This is essentially the same as creating a new object except it is immediately returned so method chaining is possible.

$view = View::factory('welcome');

Passing data into your views

Data is passed from the controller to the view by way of an an object.

Let's look at the Controller:

class Welcome_Controller extends Controller {
 
    function index()
    {
        // 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.";
 
        // Render the view
        $view->render(TRUE);
    }
}

Now open your view file and add variables:

<html>
    <head>
        <title><?php echo $title;?></title>
    </head>
    <body>
        <h1><?php echo $heading;?></h1>
        <p><?php echo $content;?></p>
    </body>
</html>

Note:

Use of arrays (CodeIgniter style) is still possible in Kohana, see more examples below

Views within views

To load views within other views:

// Example of code inside your Controller
$view = new View('template');
 
$view->header  = new View('header');
$view->content = new View('content');
$view->footer  = new View('footer');
 
$view->header->title     = 'Title of page';     // string for variable $title in view header.php
$view->content->heading  = 'Heading of your page'; // string for variable $heading in view content.php
$view->footer->copyright = 'Copyright';         // string for variable $copyright in view footer.php
 
$view->render(TRUE);

View: template.php

<?php echo $header; ?>
<?php echo $content; ?>
<?php echo $footer; ?>

View: header.php

<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>

View: content.php

  <body>
  <h1><?php echo $heading; ?></h1>

View: footer.php

  <?php echo $copyright; ?>
  </body>
</html>

Output:

<html>
  <head>
    <title>Title of page</title>
  </head>
  <body>
  <h1>Heading of your page</h1>
  Copyright
  </body>
</html>

Of course, using stylesheets and applying them to divs within your layout would give the exact design you want. You may also need custom helpers to generate navigation, breadcrumbs and dynamic content (banners, customized ads) to add a professional touch.

Note: Please also consider using the Template_Controller, this can merge the header.php and footer.php into one file.

Data scope

Rendering

Execute the render method on the view instance.

Examples

Example 1: Render on View instance

$view = new View('sample');
$view->render(TRUE);

Example 2: Force Render on View::factory

View::factory('sample')->render(TRUE);

Example 3: Inline in existing view

<?php echo View::factory('sample')->render(); ?>

See Core:View class for more information about Core:View→render parameters

Complete Example

Controller: products.php

$products = array(
    array(
        'name' => 'Product1',
        'quantity' => '3'
    ),
    array(
        'name' => 'Product2',
        'quantity' => '7'
    )
);
 
$view = new View('products/list');
$view->title = 'Products';
$view->set('products', $products);
$view->render(TRUE);

An array of product data is defined. The products list view is loaded and the variables title and products are set. The view is rendered and outputted straight to the browser.

View: products/list.php

<html>
<head>
    <title><?= $title ?></title>
</head>
<body>
    <h1><?= $title ?></h1>
    <table>
        <?php foreach ($products as $product): ?>
            <tr><td><?= $product['name'] ?></td><td><?= $product['quantity'] ?></td></tr>
        <?php endforeach; ?>
    </table>
</body>
</html>

The title variable is echo'd. The products array is iterated over and each product is echo'd within table row and column tags.

Output:

<html>
<head>
    <title>Products</title>
</head>
<body>
    <h1>Products</h1>
    <table>
        <tr><td>Product1</td><td>3</td></tr>
        <tr><td>Product2</td><td>7</td></tr>
    </table>
</body>
</html>