SiteCrafting Blah Blah Blog
Jan. 29, 2008 at 3:15pm
PHP Patterns, Part III
The TO Pattern

The Template Object (or TO) is a design pattern of my own that I developed to fill the role of the View layer in the MVC model. As you have probably figured out, the purpose of the TO is to handle everything related to the user interface. The idea here is to separate the interface as much as possible from the rest of the application, so that we could do a complete rewrite of an application without ever touching (or accidentally "breaking") the view portion.
This object was designed around a template model, in which the HTML/CSS is stored completely independent of the template object. Using this approach, the appearance of the web application and how it operates are completely decoupled. Both the view layer and the rest of the application can be completely replaced without affecting each other.
So before I bore you to death with my talking, let's take a look at some code. Below is some sample code for an abstract TemplateObject class, and then a basic FormTO class that would be useful for a basic web form:
/**
* Abstract TemplateObject Class
*/
abstract class TemplateObject {
abstract function render();
abstract function setTemplate($template);
}
/**
* Generic Form Template Object
*/
class FormTO extends TemplateObject {
protected $template;
protected $method;
protected $action;
protected $name;
/**
* Default Constructor
* @param string $method the form method (post/get)
* @param string $action the form action
* @param string $name the name of the form
* @param string $template the location of the template file
*/
public function __construct($method = post, $action = ,
$name = , $template = ) {
$this->template = $template;
$this->method = $method;
$this->action = $action;
$this->name = $name;
}
/**
* Renders the page using the currentlyl stored values.
*/
public function render() {
try {
include($this->template);
}
catch Exception($ex) {
print(The specified template was invalid.);
}
}
public function setTemplate($template) {
$this->template = $template;
}
public function setMethod($method) {
$this->method = $method;
}
protected function printMethod() {
print($this->method);
}
public function getMethod() {
return $this->method;
}
public function setAction($action) {
$this->action = $action;
}
protected function printAction() {
print($this->action);
}
public function getAction() {
return $this->action;
}
public function setName($name) {
$this->name = $name;
}
protected function printName() {
print($this->name);
}
public function getName() {
return $this->name;
}
}
As you can see this is a fairly simple setup. We have a simple Template Object that we can use to set various values for the page it reperesents. It essentially acts as an interface to the actual web page. Now lets take a look at what a template file might look like. Remember, the added felxibility here is that the template file can do anything it wants with the data that has been stored in the TO. It has full control over how the information is presented. For illustrative purposes, I will make my example as simple as possible:
<form method="<?php $this->printMethod() ?>"
action="<?php $this->printAction() ?>"
name="<?php $this->printName() ?>">
<!-- FORM CONTENT WOULD GO HERE -->
</form>
I'm sure you can imagine how easy it becomes to expand upon this simple design to build a very flexiple and complex web application in very little time. Using this approach, a developer could build a site without any concrete design in place, using a simple layout that could easily be replaced further down the road without rewriting or refactoring any code. This also allows designers to build an interface without having to understand the backend structure of the application. Pretty handy if you ask me!
Posted in Coding Techniques, From the Workbench, PHP, Software Engineering by Nick Williams
Comments (0)
Add your comment below