This is documentation for Kohana v2.3.x.

Table of Contents
StatusStub
TodoWrite 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 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 .= '<br><a href="test_2">page 2</a>';
 
 		// 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 .= '<br><a href="index">page 1</a>';
 		// 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

<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body>
  <?php echo $content ?>
 
  <?php echo $copyright; ?>
  </body>
</html>

Save this as /application/views/test.php

<h1><?php echo $heading; ?></h1>
<p><?= $content ?></p>

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 = "&#169; Me, 2008";
 
		// Look:
		$this->template->menu = new View('test_menu');
	}

Create a new view and save it as /application/views/test_menu.php

<div style="width: 100px; float: right; border: 1px solid lightgreen;">
<ul>
	<li>menu 1</li>
	<li>menu 1</li>
</ul>
</div>

Alter /application/views/base_page.php to display the menu:

<html>
  <head>
    <title><?php echo $title; ?></title>
  </head>
  <body>
  <?php echo $menu ?>
  <?php echo $content ?>
 
  <?php echo $copyright; ?>
  </body>
</html>

Obviously you'll need to add some meaningful content to the views :-)