// 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:**
// 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
**View:** header.php
**View:** content.php
**View:** footer.php
**Output:**
Title of page
Heading of your page
Copyright
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 [[addons:template|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
render(); ?>
See [[core:view|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
= $title ?>
= $title ?>
= $product['name'] ?> = $product['quantity'] ?>
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:**
Products
Products
Product1 3
Product2 7