In the last Kohana tutorial, we learn about 2nd part of MVC: views. This tutorial we will discuss further (but not quite in depth) on view.
To display dynamics in your webpages through views, you should have the values been prepared by controller and passed forward to view in its display form. In view, you should be prohibited to perform logical operations and avoid formatting operations. For example, to display age of an user, do not query database for his Date of Birth in view, avoid passing date to then be formatted in view, instead you should have the display format prepared by controller and pass that view that can be simply printed out from it. This is the fundamental to separation of concerns.
There are 2 methods to assign variables to a view: through set() method and bind() method. The difference is set() is passing by value whereas bind() is passing by reference. This means bind() has the following feature:
- Passing by reference means not as memory exhaustion when dealing with complex objects
- You may modify the object after it has been binded to a view.
Update page controller
open up \application\classes\controller\page.php and apply usage of variable setting and bindings:
class Controller_Page extends Controller { public function action_about() { //-- Arrange $page_title = "About Us - Kohana Demo Site"; $page_header = "All About Us"; $message = "This is where you learn about this website."; //-- Render the view $view = View::factory('page/about'); $view->set("title", $page_title); $view->set("header", $page_header); $view->set("message", $message); $this->response->body($view); } public function action_contact() { //-- Arrange $page_title = "Contact Us - Kohana Demo Site"; $page_header = "Contact Information"; $message = "I will let you know how to find us in this page."; //-- Set Contact Details to an Anonymous Object $contact_info = new StdClass; $contact_info->telephone = "1800 111 888"; $contact_info->address = "123 Fake st. Springfield"; $contact_info->email = "demo@demosite.com"; //-- Render the view $view = View::factory('page/contact'); $view->set("title", $page_title); $view->set("header", $page_header); $view->set("message", $message); $view->bind("contact_info", $contact_info); $this->response->body($view); } } // End Page
Update views
Now updates their views respectively.
\application\views\page\about.php
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $header; ?></h1>
<p><?php echo $message; ?></p>
</body>
</html>\application\views\page\contact.php
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $title; ?></title>
</head>
<body>
<h1><?php echo $header; ?></h1>
<p><?php echo $message; ?></p>
<ul>
<li>Telephone: <?php echo $contact_info->telephone; ?></li>
<li>Address: <?php echo $contact_info->address; ?></li>
<li>Email: <?php echo $contact_info->email; ?></li>
</ul>
</body>
</html>
In addition
There is a third method to assign variables to a view which you should avoid. That is global setting/binding by set_global() and bind_global() methods. The concept of global variables in programming language, it is a variable that can be accessed through all scopes in a design. This has been fraud upon has it promotes laziness oppose to appropriately layer your design.
Download
Summary
This is the day 5 of Kohana tutorial series, where we covers usage of variables in view.


