Kohana PHP 2.3.x Tutorial Part II
Welcome to the second tutorial in this series on how to develop with Kohana PHP. If you haven’t read the first part, I would read Part 1 before going on. In this tutorial we will be going over views, wonderful views, glorious views.
Let’s just dive straight into building our first picturesque view.
Create the file “myfirstkohana/application/views/hello.php” and put the following into it:
<html> <head> <title>Hello!</title> </head> <body> <h1>This is my first view</h1> <?php echo $content;?> </body> </html>
Looking at the above, it’s pretty straight forward and simple HTML file with some simple PHP code thrown in.
Just because we put a view with the same name as our controller, it’s not going to be shown until we say so. Let’s go back to our wonderful controller “myfirstkohana/application/controllers/hello.php” and edit the index method to look like the following:
public function index()
{
$view = new View('hello');
$view->content = 'Hello World!';
$view->render(TRUE);
}Let go through this line by line. The first line:
$view = new View('hello');tells the framework to create a new “view” object and load the file :myfirstkohana/application/views/hello.php” into it. The next line:
$view->content = 'Hello World!';
creates a new property called “content” in the object that has the value “Hello World!”. The last line
$view->render(TRUE);
compiles the template with our assigned values and outputs it to the screen. What we should get when we load “http://youserver/myfirstkohana/hello” is “This is my first view” and below that should read “Hello World!”.
This might the simplist view you’ll see. Of course, I don’t think we’ll be wanting something so simple, so lets knock it up notch and create views with nested views.
Create the file “myfirstkohana/application/views/default_template.php” and put this in it:
<?php echo $header; ?> <?php echo $content; ?> <?php echo $footer; ?>
The above code is just a master template, or the template of templates if you will. Next lets create “myfirstkohana/application/views/default_header.php” and put the following into it:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" > <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title><?php echo $pageTitle;?></title> </head>
and then “myfirstkohana/application/views/default_content.php”:
<body> <h1>This is my second view</h1> <?php echo $content;?>
finally “myfirstkohana/application/views/default_footer.php”:
</body> </html>
Above we taken a basic HTML page and split it up “logically” into a header, footer and a content section. Let open our controller to build out our more complex view. Edit “myfirstkohana/application/controllers/hello.php” to make the index method look like this:
public function index()
{
$view = new View('default_template');
$view->header = new View('default_header');
$view->content = new View('default_content');
$view->footer = new View('default_footer');
$view->header->pageTitle = 'I am on the top';
$view->content->content = 'Hello World!';
$view->render(TRUE);
}That wasn’t too hard was it? Basically we load our first view, which is the main template. Then we populate our properties with objects of our inner templates. It’s like working with multi-demential arrays in a sense.
With the ability to nest views/templates inside each other, things are a lot nicer to deal with. There is a problem thou. Each method (action) that needs to have a templated view, you need to do what we did to our index action to each one. Kohana does have an answer.
The Template_Controller allows to give a default view to actions, which if you have a lot of actions for a controller, it can really cut down on development and debugging time. Let go ahead and re-work out the hello controller (“myfirstkohana/application/controllers/hello.php”).
<?php
defined('SYSPATH') or die('No direct access allowed.');
class Hello_Controller extends Template_Controller
{
public $template = 'default_template'; // Default template (view) to use
public $auto_render = TRUE; // Auto render template after controller is done
public function __call($method, $arguments)
{
$this->name($method);
}
public function __construct()
{
parent::__construct(); // Has to be here!
// Load the inner templates
$this->template->header = new View('default_header');
$this->template->content = new View('default_content');
$this->template->footer = new View('default_footer');
$this->template->header->pageTitle = 'My first Kohana App';
}
public function index()
{
// Put something useful in our variables.
$this->template->header->pageTitle .= ' ::: I am on the top';
$this->template->content->content = 'Hello World!';
}
public function america()
{
$this->template->content->content = 'Hello America!';
}
public function name($name)
{
$this->template->content->content = 'Hello ',$name,'!';
}
}Here we have defaulted our main template, so now we didn’t have to declare it in each method. Also in our constructor method, we loaded the sub templates in and gave a default value to one of the template variable that used in the title tag. In our index method we added on to the title tag (think SEO) and put some generic content in place.
Stay tuned for the third part of our series and find out about models and working with external data!





May 27, 2009 at 10:13 am
May 27, 2009 at 10:14 am
May 27, 2009 at 10:17 am
June 3, 2009 at 2:02 am
July 6, 2009 at 8:24 am
July 6, 2009 at 8:31 am
July 6, 2009 at 12:05 pm
July 29, 2009 at 12:42 am
August 28, 2009 at 3:49 pm
November 11, 2009 at 1:01 pm
November 12, 2009 at 5:19 am
November 12, 2009 at 9:03 am