Welcome to the second part in this series on how to develop with Kohana PHP V3 (KO3). If you haven’t read the first part, I would read it before going on. In this tutorial we will be going over how to develop views.
Before we get into views, we’ll want to update the KO3 code base, So point your browser to http://dev.kohanaphp.com/projects/kohana3/files, download it, open it and extract everything inside the “kohana” folder to “mykohana3″. Once you have done that, delete or rename the “install.php” in your “mykohana3″ folder. Next, open up the “bootstrap.php” file in the “application” folder and change the following line:
[php]Kohana::init(array(‘base_url’ => ‘/kohana/’));[/php]
to
[php]Kohana::init(array(‘base_url’ => ‘/mykohana3/’,
‘index_file’ => ”));[/php]
Now that we are updated to the latest and greatest, lets get right in to developing a view. Create a new folder within the “application” folder named “views” and inside the “views” folder, create another folder named “pages”. Now open up a new document in your editor and put the following in it:
[php]<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>This is my first view</h1>
</body>
</html>[/php]
Now save that in your “application/views/pages” as “ko3.php”. As you can tell, it’s a pretty simple HTML page with some PHP mixed in. Lets open the “ko3″ controller (“application/classes/controller/ko3.php”). Replace the “action_index” class with the following:
[php] public function action_index()
{
$this->request->response = View::factory(‘pages/ko3′);
}[/php]
Save it, and load up “http://yourserver/myfirstkohana3/” in your browser. You should see “This is my first view”. The code above is pretty simple, we use the “Facotry” method of the view class to load the file “application/views/pages/ko3.php”, renders it and outputs it. Not very exciting, so let go back to the view (“application/views/pages/ko3.php”) and add:
[php] <?php echo $content;?>[/php]
After the “h1″ tags. Your view should look like this:
[php]<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>This is my first view</h1>
<?php echo $content;?>
</body>
</html>[/php]
If we were to refresh the browser, we would seen an exception error about an undefined variable. So let’s go ahead and fix that, by relating data to the view variable in our controller. So, change up “Action_Index” controller to look like this:
[php] public function action_index()
{
$view = View::factory(‘pages/ko3′);
$view->content = ‘We have data!’;
$this->request->response = $view->render();
}[/php]
Now if we refresh the browser, we should see “This is my first view” and under that “We have data!”. Lets explain the the code line by line.
[php]$view = View::factory(‘pages/ko3′);[/php]
This load our view file (“application/views/pages/ko3.php”) into the view controller.
[php]$view->content = ‘We have data!’;[/php]
This assigns a variable for use by the view called “content” and we also assign data to that, in this case it’s “We have data!”
[php]$this->request->response = $view->render();[/php]
This renders the view and outputs it.
Simple enough, but there is another way of doing the above. We could have also done the following:
[php] public function action_index()
{
$data['content'] = ‘We have data!’;
$view = View::factory(‘pages/ko3′, $data);
$this->request->response = $view->render();
}[/php]
Basically the above use an array with element keys as the template variable names to assign the data. We are not done yet! There’s 2 more ways we can do the above.
[php] public function action_index()
{
$view = View::factory(‘pages/ko3′)
->set(‘content’, ‘We have data!’);
$this->request->response = $view->render();
}[/php]
The above used the set method of the view class, which you can use method chaining to set all your template variables. And now for forth way:
[php] public function action_index()
{
$content = ‘We have data!’;
$view = View::factory(‘pages/ko3′)
->bind(‘content’, $content);
$this->request->response = $view->render();
}[/php]
Now the above is using the bind method of the view class. Once again, this you can do method chaining here. This is a little bit different from the set method, since it will create a reference to a variable. So $content might equal ‘We have data!’ when we bound the variable to the template variable, but could change later on, since we are referencing the variable $content in our controller, instead of assigning data to the template variable.
If we were to do the following:
[php] public function action_index()
{
$content = ‘We have data!’;
$view = View::factory(‘pages/ko3′)
->bind(‘content’, $content);
$content = ‘Our data changed’;
$this->request->response = $view->render();
}[/php]
Instead of “We have data!” showing on our screen, we would have “Our data changed”.
Now lets do a view with in a view! Create a new folder in your “application/views/” named “blocks”. Lets create another view file named “ko3_inner.php” and put this in it:
[php] <h3>This is an inner view</h3>[/php]
Save that in your “application/views/blocks/” directory. Now lets edit the “ko3″ view (“application/views/pages/ko3.php”) and add the following:
[php] <?php echo View::factory(‘blocks/ko3_inner’)->render(); ?>[/php]
You view should like this:
[php]<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>This is my first view</h1>
<?php echo $content;?>
<?php echo View::factory(‘blocks/ko3_inner’)->render(); ?>
</body>
</html>[/php]
So if we run it we should see what we had before, then “This is an inner view”. This would be useful for static content, but we won’t be able to use variables directly to that view. So let fix this. Lets go back to our controller (“application/classes/controllers/ko3.php”) and edit the “Action_Index” method to look like this:
[php] public function action_index()
{
$ko3_inner['content'] = ‘We have more data’;
$ko3['content'] = ‘We have data’;
$ko3['ko3_inner'] = View::factory(‘blocks/ko3_inner’, $ko3_inner)
->render();
$view = View::factory(‘pages/ko3′, $ko3);
$this->request->response = $view->render();
}[/php]
This will render the view to a array that then is rendered by the main view. If you noticed I did the inner stuff first and went back to using array template variable setting style. Next we will need to edit the main view (“application/views/pages/ko3.php”). The line we put in before, we will change it to:
[php] <?php echo $ko3_inner; ?>[/php]
The view should look like this:
[php]<html>
<head>
<title>Hello!</title>
</head>
<body>
<h1>This is my first view</h1>
<?php echo $content;?>
<?php echo $ko3_inner; ?>
</body>
</html>[/php]
And the last change we need to make is to the inner view (“application/views/blocks/ko3_inner.php”). Make it look like this:
[php] <h3>This is an inner view</h3>
<?php echo $content;?>[/php]
If you refresh the browser after saving, you should now see the following:
[php]This is my first view
We have data
This is an inner view
We have more data[/php]
Pretty cool, now you can make your views more modular and reusable. Now lets get into making variables globally available to your views. Back to your controller (“applications/classes/controllers/ko3.php”) and edit the “Action_Index” method so the following is at the top of the method:
[php] View::set_global(‘x’, ‘This is a global variable’);[/php]
So now the method should look like this:
[php] public function action_index()
{
View::set_global(‘x’, ‘This is a global variable’);
$ko3_inner['content'] = ‘We have more data’;
$ko3['content'] = ‘We have data’;
$ko3['ko3_inner'] = View::factory(‘blocks/ko3_inner’, $ko3_inner)
->render();
$view = View::factory(‘pages/ko3′, $ko3);
$this->request->response = $view->render();
}[/php]
Now if you were to edit your views and add in:
[php] <br/><?php echo $x;?>[/php]
You should now see “This is a global variable” two times on the page. As you can see this could be very handy. Basically, we are using the static method of “set” from the view class, so it ends up being available to all view object instances. You can also use the static method of “bind” to reference variables. This could be handy for dry things “DRY” and could be used in other place, like a contruct.
Untill next time, when I go over “templates” (advanced views and controller stuff), happy coding!
Sources used: Unofficial Kohana 3 Wiki
December 7, 2009 at 10:59 am
December 7, 2009 at 11:35 pm
December 11, 2009 at 2:53 pm
December 11, 2009 at 5:50 pm
December 11, 2009 at 6:42 pm
December 14, 2009 at 8:47 am
December 22, 2009 at 11:36 am
December 24, 2009 at 7:24 pm
December 28, 2009 at 8:18 am
December 30, 2009 at 10:10 am
December 31, 2009 at 10:20 pm
January 2, 2010 at 2:02 am
January 13, 2010 at 7:29 am
February 10, 2010 at 11:43 am
February 10, 2010 at 11:59 am
May 7, 2010 at 4:45 am
June 5, 2010 at 10:55 am
July 25, 2010 at 8:18 am
August 10, 2010 at 10:18 pm
September 23, 2010 at 9:56 am
July 27, 2011 at 7:04 pm
November 18, 2010 at 1:50 pm
November 29, 2010 at 3:59 am
December 27, 2010 at 4:41 pm
January 21, 2011 at 4:48 pm
January 24, 2011 at 3:20 pm
January 29, 2011 at 5:53 am
March 26, 2011 at 6:42 am
February 13, 2011 at 4:50 am
February 19, 2011 at 2:16 pm
March 26, 2011 at 6:45 am
March 2, 2011 at 4:55 pm
March 9, 2011 at 2:08 am
March 19, 2011 at 2:07 am
April 18, 2011 at 2:26 am
May 4, 2011 at 4:44 pm
August 22, 2011 at 8:54 am