Zend Framework: Using View Helpers to Build Rich, Scalable, Controls

Using View Helpers to Build Rich, Scalable, Controls
by: Jon Lebensold

Whether you’re developing an ASP.NET application, working with rails or dealing with a templating engine like Smarty, the idea of partial templates is not foreign. Partial templates allow developers and designers to work with panels or sub-groupings of content that need to be dynamic.

With a proper AJAX framework in place, these controls can be written in a way that they can be updated in whichever Controller they eventually reside. This kind of flexibility can allow you the flexibility of using certain signature controls for multiple parts of an application.

For example, you could develop an AJAX comment box that would write comments for articles and blog posts but would hit different database tables based on it’s constructer. The rest of the control would remain the same, resulting in a more consistent look and feel for your users.

View Helpers are not partials in the Ruby on Rails sense since they’re is view logic associated with them and they don’t explicitly have .phtml files. View Helpers also aren’t like wordpress templates, with content broken up into different parts (a good example of this kind of compositional design will be coming in the Zend_Layout package to be released soon).

View Helpers seem to resemble the user control concept of in .NET, where a panel can be dropped into any form, with its codebehind managing any events or user-control specific display code.

In summary, View Helpers are great for encapsulating forms, grids and other functionality that could eventually be bound to a different data source or even be pushed back to the user through a simple AJAX call.

According to the samples provided in the Zend documentation, View Helpers seem to mix presentation with logic. My assumption is this was intended to limit the creation of Zend_View objects since Zend_View can load View Helpers dynamically.

The View Helper sample provided in the zend documentation looks like this:

  1.  
  2.  
  3. class My_View_Helper_SpecialPurpose
  4. {
  5.     protected $_count = 0;
  6.     public function specialPurpose()
  7.     {
  8.         $this->_count++;
  9.         $output = "I have seen ‘The Jerk’ {$this->_count} time(s).";
  10.         return htmlspecialchars($output);
  11.     }
  12. }
  13.  

While this works for simple output, where a View Helper would be interacting with the calling object (which is typically the Zend_View), this approach doesn’t lend itself to doing rich partial templating.

As long as you keep the Helpers in one phtml file, you could add a “helpers” folder to your document tree and render the View Helper like any other view. The advantage of this is that factoring out part of your template into an AJAX control becomes really trivial. Furthermore, your display and view logic are separated, and lastly, you could even test the View Helper (including its behaviour) without a particular controller action in mind.

These javascript samples could be written more elegantly by loading the javascript through a referenced javascript file file that is tailored to a particular part of an application.

The actual index.phtml page could look something like this:

  1.  
  2. <script type="text/javascript">
  3. // this is generic enough that it could be pushed into an external .js file
  4. function loadMyPost(id)
  5. {
  6.  
  7. if ($(id+’_MyPostForm’).title.value.length > 0)
  8. var params = ‘title=’+$(id+’_MyPostForm’).title.value;
  9.  
  10.  
  11. new Ajax.Request(‘/ajax/MyPost’, {
  12.  
  13.  
  14.   method: ‘post’,
  15.   parameters: params,
  16.   onSuccess: function(transport, json) {
  17.       //use and handle foo response data
  18.       $(id+’_MyPostForm’).insert(transport.responseText);
  19.  
  20.        
  21.   },
  22.   on500: function(transport) {
  23.       //handle error, inform user
  24.       alert(‘error’);
  25.   }
  26. });
  27.  
  28.  
  29. }
  30. </script>
  31. <?= $this->MyPost("");  ?>
  32. <?= $this->MyPost("");  ?>
  33.  
  34.  

Notice how I’ve called MyPost twice? each one of these view helpers will have its own javascript configuration and will fire independently of it’s predecessor, thanks to View Helpers being able to call their inheritor to see how many times they’ve been invoked on a page.

The actual helpers/MyPost.phtml page would then look like this:

  1.  
  2. <script type="text/javascript">
  3. function ajaxLoad_<?=$this->formId ?>(event)
  4. {
  5. loadMyPost(<?=$this->formId ?>);
  6. return false;
  7. }
  8. </script>
  9.  
  10. <div>
  11. <form id="<?php echo $this->formId; ?>_MyPostForm"
  12. action="/index"
  13. method="post"
  14. onsubmit="return ajaxLoad_<?=$this->formId; ?>(this);" >
  15.  
  16.  
  17.  
  18.         <?= $this->formText(‘title’, $this->title, array(‘size’ => 32) ); ?>
  19. <?= $this->formSubmit(‘POST!’, "" ); ?>
  20. <?= $this->title;  ?>
  21. </form>
  22. </div>
  23.  

Notice that the View Helper itself is using another View Helper (formSubmit) to simplify the submit button on the actual form. We could easily extend this example with lookups that would dictate the visibility of certain portions of the View Helper. This way, the View Helper is responsible for rendering logic and it can be given a set of data to render the View by, or rather, it can “call out” in the actual Zend_View_Helper_MyPost class.

MyPost.php (the code-behind for the View Helper):

  1.  
  2. class Zend_View_Helper_MyPost {
  3.     protected $_formId = 0;
  4. public function MyPost($title)
  5. {
  6. $view = new Zend_View();
  7. $view->setBasePath(BASEPATH);
  8.  
  9.  
  10. $view->formId = ++$this->formId;
  11. $view->title = $title;
  12.  
  13.  
  14. return $view->render("helpers/MyPost.phtml");
  15.  
  16.  
  17.  
  18. }
  19.  
  20.  
  21.  
  22. }
  23.  

This is an example of a form that could be dynamically posted from any page in your application.

Notice how the javascript uses the “formId” (which could be derived through using the same method as $this_count++ in the previous example) to maintain uniqueness. This enables the creation of multiple instances of the View Helper on the page, all bound to their own javascript event listeners or custom javascript.

Using prototype, you can submit directly to that particular version of the ajaxMethod or even post the View Helpers’ form to a Controller that supports Ajax Postbacks. Luckily, proposals like Zend_Controller_Action_Helper_AjaxContext intend to facilitate this process of managing how a view is rendered from a controller. In this particular example, I’ve created a custom AjaxController for my application that looks like this:

  1.  
  2. function MyPostAction()
  3. {
  4. /* [Xml response] */
  5.  
  6.  
  7. $responseDataXmlEncoded = ‘<div>’ . $this->_request->getParam(‘title’) . ‘</div>’;
  8.  
  9.        
  10. $this->getResponse()->setHeader(‘Content-Type’, ‘text/xml’)
  11.                            ->setBody($responseDataXmlEncoded);
  12.  
  13.        
  14. }
  15.  

The custom View Helper will post to MyPostAction where the standard Zend_Controller methods can be used to collect the postback and return an XML result. This way, you can easily migrate existing postback code into an AJAX-enabled view-helper. MyPostAction has been configured to return xml instead of a regular phtml template. In order for this to work, you need to disable default rendering in the init() function of the AjaxController:

  1.  
  2. function init()
  3. {
  4.       $this->_helper->viewRenderer->setNoRender();
  5. }
  6.  

The same technique can be applied to a JSON formatted response, where prototype (or any other javascript framework one of your choice) could be used to pick up the response and create a javascript “object” that’s ready for rendering.


This entry was posted on Tuesday, January 29th, 2008 at 5:31 pm and is filed under Advanced PHP, Zend-Framework. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

6 Responses to “Zend Framework: Using View Helpers to Build Rich, Scalable, Controls”

  1. […] Another article has been posted on KillerPHP. This one is about Zend View_Helpers. I’ve been struggling with a good Zend Framework architecture that provides a development roadmap where controller logic can be easily refactored into AJAX controls. At work I’ve been researching web services for AJAX integration on the ASP.NET tip of things due to the lack of user-control support for AJAX.NET controls. I’m also not very impressed with the AJAX.NET Extenders since they seem to mix data that would be gathered from a web service and its presentation on the client. […]

  2. Lee says:

    This is a great post! I am in need of partials in my project and this post set me on the right path to figuring out how to do it. I’m not using AJAX but this idea is very useful. The only thing I am doing differently is that instead of instantiating a new Zend_View and setting the basepath in my View Helper I am retrieving the View Helper that is already initialized in my View Renderer like this:

    $view = Zend_Controller_Action_HelperBroker::getExistingHelper(‘viewRenderer’)->view;

    Then you can set variables for your view scripts and render them like this:

    $view->someVar = “someValue”;
    $out = $view->render(“path/to/viewScript.phtml”);
    return $out;

    This saves you from having to create a brand new Zend_View and trying to figure out how to set the basepath in a portable way. You do need to be mindful of the fact that you aren’t creating a new namespace for your partial so be ware of variable name clashes. Since you are reusing the same view your controller is using, anything you set using $this->view->whatever will also be available in your partial view script as $this->whatever.

  3. @Lee: Thanks for mentioning this! I was looking for a way to call View Helpers without creating a whole new Zend_View or having to create custom classes that would be called from a view helper only for some instances.

  4. Elijah Insua says:

    I have used this method with various php frameworks many times. I actually prefer breaking up my templates and following a half ‘naked objects’ approach where the actual item that you are working with contains the templates and business logic.

  5. Drevak says:

    You dont need to instance a new Zend_view object, just define a setView method in your helper, like this:

    public $view;

    public function setView(Zend_View_Interface $view)
    {
    $this->view = $view;
    }

Created By Stefan Mischook  |  Top of page  go to top of page