February 16th, 2008
Update: With the release of Zend_Layout, the need to this sort of workaround has diminished.
It’s no secret that I’m a big fan of the Zend Framework. After fumbling around with a primitive MVC application at work, I was instantly won over by the clean, and intuitive way they implement MVC. Round about version 0.9, the ViewRenderer was introduced. ViewRenderer is, I’d say, 90% useful. It makes certain assumptions about the way you’re using the framework. If you color between the lines it’s a great help.
One thing I almost always have to do is override the way it selects and automatically renders templates based on controller and action names. I typically roll with a single template and load elements into that template. Of course, I could just disable ViewRenderer altogether, but it does do a lot of other things that I find helpful, like initializing the View.
My solution: extend the default Zend_Controller_Action class. There’s only a few things to do:
- Disable the auto-render functionality of ViewRenderer
- Add a new
$_template property to hold the template to render
- Render a single template in the
postDispatch method
Here’s the code:
require_once "Zend/Controller/Action.php";
class Blink_Controller_Action extends Zend_Controller_Action
{
protected $_template = "defaultTemplate.phtml";
public function init()
{
$this->_helper->viewRenderer->setNoRender(true);
parent::init();
}
public function postDispatch()
{
echo $this->view->render($this->_template);
}
}
With our new controller action, we can then largely forget about rendering the View. All we have to do is assign values to the View:
require_once "Blink/Controller/Action.php";
class MyController extends Blink_Controller_Action
{
public function IndexAction ()
{
$this->view->myValue = "some value";
// Assign more values...
// Do business logic...
// Optionally, select a new template to render
$this->_template = "differentTemplate.phtml";
}
}
February 5th, 2008
As a casual observer of the Web 2.0 boom and fizzle, I’m left to wonder if the Internet as we know it is as mature as we all think it is. However you slice it, the Internet still utilizes the same metaphor as notepad: a page. Don’t get me wrong, I don’t have anything against pages—working for a newspaper, pages keep me employed. But you have to wonder if a page is really the peak of innovation or if maybe we could come up with something better.
Does it make sense to load GMail on a page? Sure, I’m reading my email, but GMail is a full-fledged web application. I’ve never heard it called a “web page”. My newspaper can’t automatically mark the sports section as read or filter out all the unwanted ads tucked in between stories. It just sits there. Many web applications, like GMail, Yahoo! Mail, and BaseCamp, have been painstakingly built on top of this page metaphor—not necessarily because it’s the best platform for building web applications, but because the delivery mechanism is so prolific.
The changes have been slow and incremental, but there has been a steady push away from the original concept of a web page. Macromedia, er, Adobe has made tons of money with Flash. The Web 2.0 craze brought us AJAX. Java applets run on countless intranets worldwide (thankfully, less are being seen in the wild). Adobe, Apple, Microsoft, and many other companies all have plugins that let you watch video on a web page. All of these things are great examples of innovation, but if you look a little closer, they all share another common trait. Each one seeks to add a feature or some new functionality that was not part of the original page metaphor.
It’s getting crowded in the browser. Plugins are great; I love ‘em. But when you need all these extra applications running inside your web page, you have to wonder if we’re just polishing a rusty relic. Perhaps the Page has run its course and we should be looking for its successor, whatever that may be. Or perhaps we’re entering an age where the metaphor doesn’t matter. Have people become so acclimated to computer technology that tried and true metaphors like the desktop and folders are loosing their cachet? When a web page isn’t a page at all, is anything lost?