2006-11-09 23:38:02
In many web applications you need to send the same data in various different formats. Some examples we run into every day are web pages that offer a RSS feed, or a calendar that supports an iCalendar export. Many recent web services provide API’s in various formats such as JSON, PHP serialized objects, XML etc etc.
In each of these cases, the data remains the same – only the output format has changed. This presents a programming challenge to maintain the data’s independence from the output format. In modern programming the technique is commonly referred to as MVC: model, view, controller. The model represents the data we have, the view an output format or view of the data, and the controller which handles much of the logic between the data and the view.
This is where an output controller can help programmers keep their views separate from their models, but bridge the two together when necessary.
When I refer to an output controller, I’m referring to a tool or system of tools that understand how to connect data with views or output; a bridge between the two independent portions of the MVC paradigm.
Why are Output Controllers important?
For programmers, having a well defined output controller can speed up development by providing a naming scheme, or systematic way to organize your models and your views so that both can be easily connected and easy to understand. The output controller can also offer very important functionality for a web application. This bridge can offer important features for every view such as data caching or pre-compiled templates to improve performance.
On the other hand, having a poorly constructed output controller can prevent rapid application development and lead to intermixed models and views.
The first step to following the MVC paradigm is utilizing a template system for building all of the views of your data. Many templating systems exist for web languages, a couple well known systems would be Smarty and Savant.
Continued on Nov 28th, 2006.
This problem led me to developing an output controller for Savant. The
Savant template system is similar to many other template engines in
that you have to populate an object or template before it is sent to
the client. While not a very time consuming process, this is very
repetetive when you have to set a title for the html template, the rss
template, the iCalendar template etc etc.
To facilitate the need to populate every template with the same common information I use public member variables for my classes which correspond to the fields used in the template. This makes linking the two portions together very easy.
I now create every model completely independent of the view it will be
rendered in, but utilize public member variables for every field of
data each template will need.
A class to model a web page might be something like this:
class WebPage
{
public $title;
public $content;
}
Now lets examine two templates we would want to build, html and rss:
<html>
<title><?php echo $this->title; ?></title>
<body><?php echo $this->content; ?></body>
</html>
<rss>
<channel>
<title><?php echo $this->title; ?></title>
<description><?php echo $this->content; ?></description>
</channel>
</rss>
Now to output the object with the HTML template, normally one would create an instance of the template and populate variables within the template prior to output like this:
...
$template =& new Savant3();
$template->title = $webpage->title;
$template->content = $webpage->content;
$template->display('WebPage.tpl.php');
...
Fairly quick with only two member variables but when objects get larger this is a time consuming step. Instead of using this, with the output controller you can simply say:
...
Salty_Savant_OutputControl::displayRegion($webpage);
...
This automatically populates the WebPage.tpl.php template file with all the public member variables and displays the template. Very simple.
Likewise, to send the output using the WebPage_RSS.tpl.php template file:
...
Salty_Savant_OutputControl::outputTemplate('WebPage','WebPage_RSS');
Salty_Savant_OutputControl::displayRegion($webpage);
...
Jump in to an output controller Salty_Savant_OutputControl