6. The View

2.3 The View

A View is what the end user actually sees once the requested Controller-action has completed execution. Every Controller-action must return a valid View instance.

The default View class provided by Buan uses PHP scripts as the source for each individual View template. However, if you prefer to use other template engines then you can easily extend this base class to incorporate your own functionality.

2.3.1 Anatomy of a View

You can think of a View as equivalent to a node within a simple tree structure.

Right at the top of this tree sits the special GlobalView, with each of it's children stored in slots below it. The only special slot that you need to be aware of is the action slot within the GlobalView - this is where the resulting View from the Controller-action is stored.

In a typical HTML document the source of the GlobalView template might look like this:

<html>
<body>
	<div id="header">
		Our Website
	</div>
	<div id="main">
		<?php echo $this->getSlot('action')->render(); ?>
	</div>
</body>
</html>

2.3.2 The GlobalView

The GlobalView is a singleton instance of the View class which can be accessed from anywhere using:

$globalView = View::getGlobalView();

Calling this method will actually create and execute the UrlCommand /global-view/index. Therefore, to create your own GlobalView you simply need to create a GlobalView Controller with a single index() method which needs to return a View instance. For example:

/* GlobalViewController.php */

class GlobalViewController extends Controller {
	public function index($params) {
		$view = new View();
		// ... construct View as required ...
		return $view;
	}
}

And because you have full control over this Controller-action you also have the means to use your own custom Controller and View classes if required.