ONLamp.com    
 Published on ONLamp.com (http://www.onlamp.com/)
 See this if you're having trouble printing code examples


Implementing MVC in PHP: The View

by Joe Stump
01/26/2006

This is the third article in a series about understanding MVC in PHP. The first article explained basics of MVC programming in PHP. The second article showed the implementation of the Controller in MVC. The final article is Implementing MVC in PHP: The Model.

The presentation layer, as I call it, is the View, in common MVC terms. Its sole responsibility is to display information. It could care less about authenticating users, what the data is or, for the most part, where it came from. The only thing it has to worry about is how to render it and where to send it once rendered.

By default, the framework uses Smarty to render the framework. I'm not here to argue semantics, but your presentation layer should consist of a template engine of some sort and a few supporting presentation layers.

The idea is that, after the Model runs, the framework hands it off to a child of the FR_Presenter_common class. Actually, the framework uses the FR_Presenter::factory() to create the presenter. Each presenter should have a display() method that does the actual rendering. When the factory creates the presenter, it passes the presenter the instance of the model class. The presenter then gets the model's data using its getData() method. From there, the presenter is free to present that data however it sees fit.

FR_Presenter_smarty

The way I've created my Smarty presenter is a hybrid of two templates. I create a Smarty template, and the outer page template includes the model's template. The model class's $pageTemplateFile can request a particular outer template. If it does not, the default is tpl/default/templates/page.tpl. The page.tpl template then uses the {$modulePath} and {$tplFile} directives to include the model's template. All model templates should reside in modules/example/tpl/.

After assigning the variables, the controller runs Smarty's display function to render the templates. With little modification, you could wrap these calls with Smarty's built-in caching as well. By using Smarty, you could enable an output modifier to output gzipped code instead of plain HTML.

<?php

  /**
  * FR_Presenter_smarty
  *
  * @author Joe Stump <joe@joestump.net>
  * @copyright Joe Stump <joe@joestump.net>
  * @license http://www.opensource.org/licenses/gpl-license.php
  * @package Framework
  * @filesource
  */

  require_once(SMARTY_DIR.'Smarty.class.php');

  /**
  * FR_Presenter_smarty
  *
  * By default we use Smarty as our websites presentation layer (view). Smarty
  * is a robust compiling template engine with an active community.
  *
  * @author Joe Stump <joe@joestump.net>
  * @package Framework
  * @link http://smarty.php.net
  */
  class FR_Presenter_smarty extends FR_Presenter_common
  {
      private $template = null;
      private $path = null;

      public function __construct(FR_Module $module)
      {
          parent::__construct($module);
          $this->path = FR_BASE_PATH.'/tpl/'.FR_TEMPLATE;
          $this->template = new Smarty();
          $this->template->template_dir = $this->path.'/'.'templates';
          $this->template->compile_dir = $this->path.'/'.'templates_c';
          $this->template->cache_dir = $this->path.'/'.'cache';
          $this->template->config_dir = $this->path.'/'.'config';
      }

      public function display()
      {
          $path = FR_BASE_PATH.'/modules/'.$this->module->moduleName.'/tpl';;
          $tplFile = $this->module->tplFile;

          $this->template->assign('modulePath',$path);
          $this->template->assign('tplFile',$tplFile);
          $this->template->assign('user',$this->user);
          $this->template->assign('session',$this->session);

          foreach ($this->module->getData() as $var => $val) {
              if (!in_array($var,array('path','tplFile'))) {
                  $this->template->assign($var,$val);
              }
          }

          if ($this->module->pageTemplateFile == null) {
              $pageTemplateFile = 'page.tpl';
          } else {
              $pageTemplateFile = $this->module->pageTemplateFile;
          }

          $this->template->display($pageTemplateFile);
      }

      public function __destruct()
      {
          parent::__destruct();
      }
  }

?>
PHP Hacks

Related Reading

PHP Hacks
Tips & Tools For Creating Dynamic Websites
By Jack Herrington

Other Presenters

You can create other presenters, as well. I've created one called debug.php that simply displays various debugging information. You could change your model class's $presenter to debug and it would render completely differently.

Additionally, you could create a presentation layer called rest.php that outputs the model class's $data variable as well-formed XML. If your model detected a REST request, it would switch the presenter by assigning rest to $this->presenter.

<?php

  /**
  * FR_Presenter_rest
  *
  * @author Joe Stump <joe@joestump.net>
  * @copyright Joe Stump <joe@joestump.net>
  * @license http://www.opensource.org/licenses/gpl-license.php
  * @package Framework
  * @filesource
  */

  require_once('XML/Serializer.php');

  /**
  * FR_Presenter_rest
  *
  * Want to display your module's data in valid XML rather than HTML? This
  * presenter will automatically take your data and output it in valid XML.
  *
  * @author Joe Stump <joe@joestump.net>
  * @package Framework
  */
  class FR_Presenter_rest extends FR_Presenter_common
  {
      // {{{ __construct(FR_Module $module)
      /**
      * __construct
      *
      * @author Joe Stump <joe@joestump.net>
      * @access public
      * @param mixed $module Instance of FR_Module
      * @return void
      */
      public function __construct(FR_Module $module)
      {
          parent::__construct($module);
      }
      // }}}
      // {{{ display()
      /**
      * display
      *
      * Output our data array using the PEAR package XML_Serializer. This may
      * not be the optimal output you want for your REST API, but it should
      * display valid XML that can be easily consumed by anyone.
      *
      * @author Joe Stump <joe@joestump.net>
      * @return void
      * @link http://pear.php.net/package/XML_Serializer
      */
      public function display()
      {
          $xml = new XML_Serializer();
          $xml->serialize($this->module->getData());

          header("Content-Type: text/xml");
          echo '<?xml version="1.0" encoding="UTF-8" ?>'."\n";
          echo $xml->getSerializedData();
      }
      // }}}
      // {{{ __destruct()
      public function __destruct()
      {
          parent::__destruct();
      }
      // }}}
  }

?>

In the REST presentation layer I use the PEAR package XML_Serializer to output the FR_Module::$data array as valid XML. It's not extremely intuitive, but it does allow me to output my module in valid XML. I use this REST presentation layer later on in one of my applications.

Conclusion

You get the idea. The presentation layer is an extremely flexible way of displaying the model. To make things even better you can dynamically switch the presenter in the model before the controller renders the module class via the presentation layer.

Before I move on, I'd like to plant the seed for another presentation layer. How about using htmldoc in a presenter named pdf to render your module class as a PDF document?

Up Next

The next article will covering the model portion of the MVC framework, to which my framework also refers as "modules" and "module classes" up to this point. My example model will be a simple module to log people in and out of the system. After that you'll be on your own to build up my little framework into something useful!

Joe Stump is the Lead Architect for Digg where he spends his time partitioning data, creating internal services, and ensuring the code frameworks are in working order.


Return to the PHP DevCenter.

Copyright © 2009 O'Reilly Media, Inc.