Template Controller
What is a template controller?
A Template Controller is an extension of the Base Controller which has template support built in using some pre-defined before() and after() methods. Basically it can be used to wrap your view in a layout with a header, footer, sidebar, etc.
Please note: if you have a before() method in your template controller you must add parent::before(); to that method or the $this->template will not be available.
Using the template controller
Like all Controllers you create a class in the fuel/app/classes/controller directory. They need to extend the Controller_Template class and are prefixed by default by "Controller_". Below is an example of the controller "example":
class Controller_Example extends Controller_Template {
public function action_index()
{
$data = array();
$this->template->title = 'Example Page';
$this->template->content = View::factory('test/index', $data);
}
}
Example template
The template file is a great place to call up your JS, CSS, etc, structure your HTML and call view partials. It allows you to give your output structure. It is just a view file, by default the Template Controller will look here: fuel/app/views/template.php.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
<?php echo Asset::css('main.css'); ?>
</head>
<body>
<div id="wrapper">
<h1><?php echo $title; ?></h1>
<div id="content">
<?php echo $content; ?>
</div>
</div>
</body>
</html>
Changing the default template file
You can easily change the default APPPATH/views/template.php file to something different.
You must set the public variable $template (note: you don't need .php extension here) to something different, example:
class Controller_Example extends Controller_Template {
public $template = 'template_admin';
public function action_index()
{
$this->template->title = 'Example Page';
$this->template->content = View::factory('test/index', $data);
}
}