The View, as you might have guessed, is the 'V' in MVC. The View contains code that relates to presentation and presentation logic such as templating and caching. In the controller above we saw the show() method. This is the method that calls the view. The major component in the MindPHP.com MVC is the template class. The template.class.php file contains the class definition. Like the other classes, it has the registry available to it and also contains a __set() method in which template variables may be set and stored.
The show method is the engine room of the view. This is the method that loads up the template itseld, and makes the template variables available. Some larger MVC 's will implement a template language that adds a fhurther layer of abstraction from PHP. Added layers means added overhead. Here we stick with the speed of PHP within the template, yet all the logic stays outside. This makes it easy for HTML monkies to create websites without any need to learn PHP or a template language.
The template.class.php file looks like this:
<?php
Class Template {
private $registry;
private $vars = array();
function __construct($registry) {
$this->registry = $registry;
}
public function __set($index, $value)
{
$this->vars[$index] = $value;
}
function show($name) {
$path = __SITE_PATH . '/views' .'/' . $name . '.php';
if (file_exists($path) == false)
{
throw new Exception('Template not found in '. $path);
return false;
}
foreach ($this->vars as $key => $value)
{
$$key = $value;
}
include ($path);
}
}
?>