^Status|Stub| ^Todo|Write it!| ====== Template Controller ====== ==== Template Controller ==== Using the template controller you can set a template for your site. Its workings are simple. **Example: application/controllers/home.php** class Home_Controller extends Template_Controller { public $template = 'template'; //defaults to template but you can set your own view file public $auto_render = TRUE; //defaults to true, renders the template after the controller method is done public function __construct() { parent::__construct(); //necessary } public function index() { $this->template->content= 'index page in a template'; } } The example illustrates a file ''application/controllers/home.php'' which extends the template controller. The template controller can be found in ''system/controllers/template.php''. You set the template file in $template. It defaults to 'template' which is found in ''views/template.php''. Auto-render renders the template during the post_controller event which is executed after the controller. This all means you can change the template and auto-render all in realtime. For a more detailed discussion of Template [[http://learn.kohanaphp.com/2008/03/08/kohana-template-tutorial/|Learning Kohana: Template]] ====Example 1==== This is a simple example that shows the magic of the Template class. Save this as /application/controllers/test.php class Test_Controller extends Template_Controller { public $template = 'base_page'; public function __construct() { parent::__construct(); // the template page 'base_page' is loaded by // default, this is the same as uncommenting // the following line: // $this->template = new View('base_page'); // All pages have some things in common such as // the page title: $this->template->title = "Welcome to Kohana!"; $this->template->copyright = "© Me, 2008"; } function index() { // // don't forget that the __construct() is run // before this method, so the template // is set up and ready for this pages content. // // Load this page (Test) view $test = new View('test'); // now create this page (Test) $test->heading = "Test :: Index Heading"; $test->content = "Test :: Index :: content here."; $test->content .= '
page 2'; // add our content to the template view: $this->template->content = $test; // the view is auto-render by default } function test_2() { // Load this page (Test) view $test = new View('test'); // now create this page (Test) $test->heading = "Test :: test_2 :: Heading"; $test->content = "Test :: test_2 :: content here."; $test->content .= '
page 1'; // add our content to the base view: $this->template->content = $test; // the view is auto-render by default } }
This uses the following 2 views: Save this as /application/views/base_page.php <?php echo $title; ?> Save this as /application/views/test.php

To test this browse to http://127.0.0.1/Kohana/test/ and http://127.0.0.1/Kohana/test/test_2 The Template class is nice because it removes the need to split a template into two files, header and footer. Think of ''base_page'' as your base object, which ''views/test.php'' inherits from. ====Example 2==== It is easy to extend the template concept and add something more interesting. For example to add a menu alter the ''construct()'' method as follows: public function __construct() { parent::__construct(); $this->template->title = "Welcome to Kohana!"; $this->template->copyright = "© Me, 2008"; // Look: $this->template->menu = new View('test_menu'); } Create a new view and save it as /application/views/test_menu.php
Alter /application/views/base_page.php to display the menu: <?php echo $title; ?> Obviously you'll need to add some meaningful content to the views :-)